Tuesday, 15 May 2012

python - Foreign Key Resource from dynamic field -


After

I have an API end point called TrackMinResource , which minimizes data for a music track Returns have been received, in which the main artist of the track was returned as a ArtistMinResource .

  class TrackMinResource (ModelResource): Artist = fields.ForeignKey (ArtistMinResource, 'Artist', complete = true) class Meta: queryset = Track.objects.all (who went the two definitions here) RESOURCE_NAME = "track minutes' fields = [ 'id', 'artist', 'TRACK_NAME' label ',' RELEASE_YEAR 'Release_name'] Include_resource_uri = false cache = SimpleCache (public = true) def dehydration (self Bundle ): bundle.data [ 'full_artist_name'] = bundle.obj.full_artist_name () if bundle.obj.image_url = settings.NO_TRACK_IMAGE: bundle.data [ 'image_url'] = bundle.obj.image_url class ArtistMinResource (ModelResource): class Meta: querygroup = Artist.objects.all () RESOURCE_NAME = "Artists minutes' fields = [the 'id', 'Artist'] cash = SimpleCache (Public = True) def get_resource_uri (self, bundle_or_obj): return '/ API / V 1 / artist /' + str (Bndl_oar_obij. OBZ.) + '/'  

The pro is Belem, artist on the field track (formerly a ForeignKey ) Is now a model method which has changed the structure of the database to some extent main_artist , but I would like to return the API to the same data as previously performed). For this reason, I get this error:

  {"error": "model" & amp; lt; track: trackname & gt; An empty feature is 'artist' and does not allow a zero value. "}  

If I remove full = true from the 'artist' area, Add Code> TrackMinResource and instead null = true , I get zero values ​​for the artist field in the returned data.

  bundle.data [ 'artist'] = bundle.obj.main_artist ()   dehydration  are thus assigned Artists Instead of representing  dict  a  ArtistMinResource , get the name of the artist back in JSON (associate  source_uri  S, which I want). 

Any ideas how to get it in ArtistMinResource s my TrackMinResource ? I can use an ArtistMinResource which came true using the URL endpoint and asks for the ID. Get results from within TrackMinResource

dehydrate function Is there a function for? Itemprop = "text">

You can use your ArtistMinResource in dehydration of TrackMinResource (assuming that the main artistist () represents the object that represents your artist minisor):

< pre> artist_resource = ArtistMinResource () artist_bundle = Artist_resource.build_bundle (obj = bundle.obj.main_artist (), request = request) artist_bundle = artist_resource.full_dehydrate (artist_bundle) artist_json = artist_resource.serialize (request = request, data = Artist_bundle, format = 'application / jason')

artist_zone is now your full featured artist Should be a person. Apart from this, I am pretty sure that if you pass the request and it has a content-type header population then the format will not be passed.


No comments:

Post a Comment