django - TastyPie: ToManyField related resource -


i have model extends django user model:

class userprofile(models.model):     user = models.onetoonefield(user)     avatar = models.charfield(max_length=40, default='0')     activation_key = models.charfield(max_length=40, blank=true)     key_expires = models.datetimefield(default=django.utils.timezone.now)     contacts = models.manytomanyfield(user, related_name='contacts') 

as can see, there field contacts. means of field every user can have list of contacts (like in skype or social network). want use in tastypie resources. have 2 resources:

class userprofileresource(modelresource):     class meta:         queryset = userprofile.objects.all()         authentication = sessionauthentication()         authorization = djangoauthorization()         allowed_methods = ['get']         resource_name = 'profile'         excludes = ['id']         include_resource_uri = false  class userresource(modelresource):     userprofile = fields.toonefield(userprofileresource, 'userprofile', null=true, full=true)     contacts = fields.tomanyfield(userprofileresource, 'contacts', related_name='contacts', null=true, full=true)      class meta:         queryset = user.objects.all()         fields = ['first_name', 'last_name', 'email', 'date_joined', 'last_login', 'userprofile', 'contacts']         allowed_methods = ['get', 'post', 'patch']         resource_name = 'user'         detail_uri_name = 'username'         authentication = sessionauthentication()         authorization = djangoauthorization() 

everything ok, when make request, field contacts not work well. can't understand, how display list of other users in contacts field in tastypie resource. way, in django admin page can see list of contacts , can edit it.

so, realization of tastypie resource gives ability list of users added current user in own contacts list. need list of contacts of current user. wrong?

since contacts on userprofile model instead of user model, associated resource field should go on userprofileresource instead of userresource.

in case, i'd recommend putting contact on custom user object instead of profile model; it'll simplify code , save db joins if make related user instead of related table that's related user.


Comments