发布数据同时创建相关的Tastypie资源? [英] Posting data to create related Tastypie resources simultaneously?
问题描述
p> models.py
class A(models.Model):
name = models.CharField(max_length = 5)
class B(models.Model):
name = models.CharField(max_length = 5)
a = models.ForeignKey(A)
给定(潜在非最佳)Tastypie资源:
api.py
class AResource(ModelResource):
bs = fields.ToManyField('projectname.api.BResource','bs',full = True)
class Meta:
queryset = A.objects.all()
class BResource (ModelResource):
a = fields.ToOneField(AResource,'a',full = True)
class Meta:
queryset = B.objects.all()
让我们假设数据库是空的。现在我有相关的外部数据,并希望将数据库与A的一个实例和B的几个实例一起聚集。
什么是最漂亮的Tastypionic方法这个问题?是否有可能同时挤满A和B?或者我需要先A人群,然后将B的A的ID作为ForeignKey提供给人群。
如果有人可以提出一个示例(使用例如一个python字典和httplib2或curl)。
感谢一百万。
解决方案在这里。使用自动填充反向关系的tastypie字段的相关名称,同时创建多个对象。
http:// django-tastypie.readthedocs.org/en/v0.10.0/fields.html#tastypie.fields.RelatedField.related_name
RelatedField.related_name
用于在创建数据时自动填充反向关系。默认为无。
为了使此选项正常工作,另一个资源中必须有一个字段作为属性/ instance_name。通常这只是意味着添加一个反映的ToOneField指向。
示例:
class EntryResource(ModelResource):
authors = fields.ToManyField('path.to.api.resources.AuthorResource','author_set',related_name ='entry')
class Meta :
queryset = Entry.objects.all()
resource_name ='entry'
class AuthorResource(ModelResource):
entry = fields.ToOneField(EntryResource,条目')
class Meta:
queryset = Author.objects.all()
resource_name ='author'
使用 related_name
执行任务。它映射相关字段的对象,并在创建数据时自动填充关系。
与 full = True
在资源的两边,它将生成最大递归深度超出异常,因为这两个资源在彼此之间是完整的。
Given two related Django models A and B in a OneToMany relationship:
models.py
class A(models.Model):
name = models.CharField(max_length=5)
class B(models.Model):
name = models.CharField(max_length=5)
a = models.ForeignKey(A)
And given (potentially non-optimal) Tastypie resources:
api.py
class AResource(ModelResource):
bs = fields.ToManyField( 'projectname.api.BResource', 'bs', full = True)
class Meta:
queryset = A.objects.all()
class BResource(ModelResource):
a = fields.ToOneField( AResource, 'a', full = True)
class Meta:
queryset = B.objects.all()
Let's assume the database is empty so far. Now I have related external data, and would like to crowd the database it with both an instance of A and several instances of B.
What is the prettiest Tastypionic way to approach this problem? Is it possible to crowd both A and the Bs at once? Or do I need to crowd first A, and then crowd B supplying A's ID as the ForeignKey?
It would be great if someone could come up with an post example (using e.g. a python dictionary and httplib2, or curl).
Thanks a million.
The solution is here . Use the related name for tastypie fields which automatically populate the reverse relationship while creating multiple objects at once. http://django-tastypie.readthedocs.org/en/v0.10.0/fields.html#tastypie.fields.RelatedField.related_name
RelatedField.related_name
Used to help automatically populate reverse relations when creating data. Defaults to None.
In order for this option to work correctly, there must be a field on the other Resource with this as an attribute/instance_name. Usually this just means adding a reflecting ToOneField pointing back.
Example:
class EntryResource(ModelResource):
authors = fields.ToManyField('path.to.api.resources.AuthorResource', 'author_set', related_name='entry')
class Meta:
queryset = Entry.objects.all()
resource_name = 'entry'
class AuthorResource(ModelResource):
entry = fields.ToOneField(EntryResource, 'entry')
class Meta:
queryset = Author.objects.all()
resource_name = 'author'
Use of related_name
do the task. it maps the objects of related fields and automatically populates the relations when creating data.
as you did full=True
on both side of your resources it will generate maximum recursion depth exceeded exception because both resources are full in each others.
这篇关于发布数据同时创建相关的Tastypie资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!