Django Tastypie creating new resource that has foreign keys?
Asked Answered
H

2

10

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?

Heidiheidie answered 2/3, 2012 at 17:4 Comment(5)
Do you have the User resource as well?Approach
II'm using django-registration and just have a UserProfile model that just maps one to one to the UserHeidiheidie
Tastypie works on Resources rather than on Models, so for "/api/user/1/" to point to the User you also need a User resource, the model won't be enough. I hope that helps :)Approach
I have a User resource but it still doesn't work. I've updated this in the questionHeidiheidie
have you made any progress with this?? I was also looking to integrate django-registration with tastypieCraw
A
14

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.

Approach answered 2/3, 2012 at 19:41 Comment(0)
S
4

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()
Sthenic answered 10/2, 2013 at 11:23 Comment(1)
This is identical to kgr's answer. ForeignKey is an alias for ToOneField.Matter

© 2022 - 2024 — McMap. All rights reserved.