2012-03-02 15 views
10

私はTastypieの仕事で新しいインスタンスを作成しようとしていますが、私は外来キーでこのエラーが発生し続けています。外部キーを持つ新しいリソースを作成するDjango Tastypie?

モデル::

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 

モデルリソースは(私は私自身の認証方式を使用):jQueryのを使用して

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() 

私のAPIコール:

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 
}); 

そして、ここで私のものです私が得るエラー:

(1048、\ "列 'created_by_user_id' をNULLにすることはできません\")私はここで何かをしないのです

+0

ユーザーリソースも持っていますか? – kgr

+0

私はdjango-registrationを使用していて、ユーザーに1対1でマップするUserProfileモデルを持っています。 –

+0

Tastypieはモデルではなくリソース上で動作するので、/ api/user/1 /また、Userリソースが必要な場合、モデルでは十分ではありません。私は助けることを願っています:) – kgr

答えて

14

私は関係フィールドの定義である、このようなものが動作するはず何が必要だと思う:

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() 

私がチェックしていると似フィールド定義せずに、私はちょうどあなたのようなエラーが発生します。

4

これも機能します。ここで説明されている通り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() 
+4

これは、kgrの答えと同じです。 [ForeignKeyはToOneFieldのエイリアスです](http://django-tastypie.readthedocs.org/en/latest/fields.html#foreignkey)。 – dbn

関連する問題