Kısa bir süre önce potansiyel bir akıllı telefon uygulaması için api açmak için tastypie kullanmaya başladım.json sonucunda ForeignKey gösterilmiyor tattypie api
kullanıyorum piton-2.7 ve Django-1.1.2
İki şey beni kafa karıştırıcı
1 gibidir: EntryResource
sınıfta ForeignKey
çağrılırken, onlar sadece birlikte kaynak çağrı kaynak_adı, böyle bir şekilde yaptığımda aşağıdaki geri dönüşümü alırım.
691. assert isinstance(to, basestring), "%s(%r) is invalid. First parameter to ForeignKey must be either a model, a model name, or the string %r" % (self.__class__.__name__, to, RECURSIVE_RELATIONSHIP_CONSTANT)
Exception Type: AssertionError at /api/v1/activity-stream-item-subject/
Exception Value: ForeignKey(<class 'api.resources.UserResource'>) is invalid. First parameter to ForeignKey must be either a model, a model name, or the string 'self'
: Ben
EntryResource
user = fields.ForeignKey(UserResource, 'user')
değiştirmek, o zaman ancak JSON yanıt olarak ilgili bilgileri göremez, bir traceback olmadan iyi çalışır.
API/resources.py
class UserResource(ModelResource):
class Meta:
queryset = User.objects.all()
resource_name = 'user'
excludes = ['email', 'password', 'is_active', 'is_staff', 'is_superuser']
filtering = {
'username': ALL,
}
class EntryResource(ModelResource):
user = fields.ForeignKey(UserResource, 'user')
class Meta:
queryset = Entry.objects.all()
resource_name = 'entry'
authorization = Authorization()
filtering = {
'user': ALL_WITH_RELATIONS,
'pub_date': ['exact', 'lt', 'lte', 'gte', 'gt'],
}
bir fikir?
true geçerli değil python – josephmisiti