我有以下美味的资源:
class ArtistResource(SearchableResource):
audio_tracks = fields.ToManyField('music.api.AudioTrackResource', 'audio_tracks', related_name='artists', null=True, blank=True)
class Meta:
filtering = {
"title": ('startswith',),
"id": ALL_WITH_RELATIONS,
}
queryset = Artist.objects.all()
resource_name = 'artist'
allowed_methods = ['get']
class AudioTrackResource(SearchableResource):
artists = fields.ToManyField('music.api.ArtistResource', 'artists', null=True, blank=True)
class Meta:
filtering = {
"artists": ALL_WITH_RELATIONS,
}
queryset = AudioTrack.objects.all()
resource_name = 'audio_track'
allowed_methods = ['get']
以及以下骨干模型:
class Track extends Backbone.RelationalModel
urlRoot: TRACK_API
relations: [
{
type: Backbone.HasMany
key: 'artists'
relatedModel: Artist
collectionType: ArtistCollection
reverseRelation:
key: 'track'
includeInJSON: 'title'
}
]
Track.setup()
Track
class Artist extends Backbone.RelationalModel
urlRoot: ARTIST_API
relations: [
{
type: Backbone.HasMany
key: 'audio_tracks'
relatedModel: Track
collectionType: TrackCollection
reverseRelation:
key: 'artist'
includeInJSON: 'title'
}
]
Artist.setup()
Artist
而且我无法在我的 Tastypie 资源中设置full=True,因为它会创建递归……我根本不想设置 full=True。但我不知道何时何地fetchRelated()。它不会获取任何东西,因为对象 URI 数组已经存在。有没有其他方法可以在双方都填充与 JSON 的多对多关系?
PS我在谷歌搜索这个问题3天后创建了这个问题,但我没有找到有效的解决方案。