2012-05-15 11 views
6

Sto creando UserResource e UserProfileResource. Quando creo un nuovo utente, vedo che anche la mia tabella del database dei profili utente viene aggiornata. Che è buono. Ma quando ho ottenere l'utente ottengo un errore che dice:Tastypie foreignkey impostato su null

The model '' has an empty attribute 'profile' and doesn't allow a null value. 

Il mio codice:

resources.py

class UserProfileResource(ModelResource): 
    home_address = fields.CharField(attribute='home_address') 
    user = fields.ToManyField('resources.UserResource', attribute='user', related_name='profile') 
    class Meta: 
     queryset = UserProfile.objects.all() 
     resource_name = 'profile' 
     allowed_methods = ['get', 'post', 'delete', 'put'] 
     fields = ['home_address'] 
     authorization = Authorization() 
     include_resource_uri = False 
     include_absolute_url = False 

class UserResource(ModelResource): 
    profile = fields.ToOneField('resources.UserProfileResource', attribute = 'profile', related_name='user', full=True) 
    class Meta: 
     queryset = User.objects.all() 
     resource_name = 'user' 
     allowed_methods = ['get', 'post', 'delete', 'put'] 
     fields = ['username'] 
     filtering = { 
       'username': ALL, 
       } 
     authorization = Authorization() 

    def obj_create(self, bundle, request=None, **kwargs): 
     try: 
      bundle = super(UserResource, self).obj_create(bundle, request, **kwargs) 
      bundle.obj.set_password(bundle.data.get('password')) 
      bundle.obj.save() 
     except IntegrityError: 
      raise BadRequest('IntegrityError') 
     return bundle 

models.py

class UserProfile(models.Model): 
    user = models.ForeignKey(User, unique=True) 
    home_address = models.TextField() 

Chiunque tutte le idee? johnoc

risposta

15

Dopo molte ricerche ho finalmente ottenuto una risposta a questo.

models.py

class UserProfile(models.Model): 
    user = models.OneToOneField(User) 
    home_address = models.TextField() 

resources.py

class UserProfileResource(ModelResource): 
    class Meta: 
     queryset = UserProfile.objects.all() 
     resource_name = 'profile' 
     allowed_methods = ['get', 'post', 'delete', 'put'] 
     fields = ['home_address'] 
     authorization = Authorization() 
     include_resource_uri = False 
     include_absolute_url = False 

class UserResource(ModelResource): 
    profile = fields.ToOneField('resources.UserProfileResource', attribute = 'userprofile', related_name='user', full=True, null=True) 
    class Meta: 
     queryset = User.objects.all() 
     resource_name = 'user' 
     allowed_methods = ['get', 'post', 'delete', 'put'] 
     fields = ['username'] 
     filtering = { 
       'username': ALL, 
       } 
     authorization = Authorization() 

    def obj_create(self, bundle, request=None, **kwargs): 
     try: 
      bundle = super(UserResource, self).obj_create(bundle, request, **kwargs) 
      bundle.obj.set_password(bundle.data.get('password')) 
      bundle.obj.save() 
     except IntegrityError: 
      raise BadRequest('IntegrityError') 
     return bundle 

Questo funziona. Si noti che il campo del modello è stato modificato in OneToOneField e che l'attributo resources è ora "userprofile". Spero che questo aiuti qualcuno là fuori!

John

+0

Grazie! Risparmiato qualche ora per me. :) In suvated. –

+0

@johnoc, grazie per questo, mi ha aiutato molto! – noahandthewhale

+0

Buon lavoro .... continua così. :) – CrazyGeek

Problemi correlati