Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Tastypie creating new resource that has foreign keys?

I'm trying to make creating new instances with Tastypie work, but I keep getting this error with the foreign keys. Here is my stuff:

Models:

class SuggestionVote(models.Model):
    created_by_user = models.ForeignKey(User)
    date_created = models.DateTimeField(auto_now_add = True)
    suggestion = models.ForeignKey(Suggestion)

class Suggestion(models.Model):
    title = models.TextField(blank=True,null=True)
    created_by_user = models.ForeignKey(User)
    date_created = models.DateTimeField(auto_now_add = True)
    votes = models.IntegerField(default=0)    

    def __unicode__(self):
        return self.title

Model resources (I use my own authentication method):

class UserResource(ModelResource):
    class Meta:
        list_allowed_methods = ['get']
        queryset = User.objects.all()
        resource_name = 'user'
        authentication = MyBasicAuthentication()
        authorization = DjangoAuthorization()
class SuggestionResource(ModelResource):
    class Meta:
        list_allowed_methods = ['get']
        queryset = Suggestion.objects.all()
        resource_name = 'suggestion'
        authentication = MyBasicAuthentication()
        authorization = DjangoAuthorization()

class SuggestionVoteResource(ModelResource):
    class Meta:
        list_allowed_methods = ['get', 'post']
        detail_allowed_methods = ['get', 'post', 'put', 'delete']
        queryset = SuggestionVote.objects.all()
        resource_name = 'suggestionvote'
        authentication = MyBasicAuthentication()
        authorization = DjangoAuthorization()

My API call using jQuery:

var data = JSON.stringify({
    "suggestion": "/api/suggestion/1/",
    "created_by_user": "/api/user/1/"
});

$.ajax({
  url: 'http://127.0.0.1:8000/api/suggestionvote/',
  type: 'POST',
  contentType: 'application/json',
  data: data,
  dataType: 'json',
  processData: false
});

And the error I get:

(1048, \"Column 'created_by_user_id' cannot be null\")

Am I missing something here?

like image 967
ip. Avatar asked Mar 02 '12 17:03

ip.


2 Answers

I think what you need is the definition of the relationship field, something like this should work:

from tastypie import fields

class SuggestionResource(ModelResource):
    # the relationship 
    created_by_user = fields.ToOneField( UserResource, 'created_by_user', full = True )

    class Meta:
        list_allowed_methods = ['get']
        queryset = Suggestion.objects.all()
        resource_name = 'suggestion'
        authentication = MyBasicAuthentication()
        authorization = DjangoAuthorization()

I have checked and without similiar field definition I get an error just like yours.

like image 166
kgr Avatar answered Oct 14 '22 18:10

kgr


This works too. As explained here in this Tastypie Tutorial

from tastypie import fields

class SuggestionResource(ModelResource):
    # the relationship 
    created_by_user = fields.ForeignKey( UserResource, 'created_by_user')

    class Meta:
        list_allowed_methods = ['get']
        queryset = Suggestion.objects.all()
        resource_name = 'suggestion'
        authentication = MyBasicAuthentication()
        authorization = DjangoAuthorization()
like image 27
Intrinsic Avatar answered Oct 14 '22 20:10

Intrinsic