[英]Django models created twice?
为什么我的属性模型在数据库中保存了两次? 奇怪的...
这是我的查看代码:
@login_required(login_url='/login/')
@transaction.atomic()
@reversion.create_revision()
def add_item_type(request, id_item, id_item_type):
item = Item.objects.get(id=id_item)
item_type = ItemType.objects.get(id=id_item_type)
item.status = Item.DEPLOYED # Set to phase deployment
item.save()
for a in item_type.attribute_types.all(): # Create all attribute skeletons to item
Attribute.objects.create(name=a.name, description=a.description, type=a.attr_type, item=item)
ctx = {'item':item, 'item_type':item_type}
return render_to_response('des/item/add_item_type.html', ctx, context_instance=RequestContext(request))
编辑:
当查看数据库(postgres)时,我发现视图的循环被调用了两次(不在同一循环中保存两次),或者视图被调用了两次。 我不知道为什么 我很确定这不是循环问题,因为在我的数据库中,Attribute类是这样保存的。
id - name
1 - fly
2 - sit
3 - run
4 - fly
5 - sit
6 - run
有什么想法吗? 为了理解我的代码,请参阅ItemType作为类,AttributyType作为ItemType类的属性,并且Item是ItemType的实例化,而Attribute是AttributeType。
好的,我通过在Attribute模型(Attribute.name)中分配一个unique = True值解决了我的问题,因此在同一Item类上没有相同的Attribute。 我所做的只是一个“补丁”,但并不能解决真正的问题,该视图被两次调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.