为什么我的属性模型在我的数据库中被保存两次?奇怪...Django模型创建两次?
这里是我的视图代码:
@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的实例。
好吧,你有一个循环。你为什么不在shell中检查'ItemType.objects.get(id = id_item_type)'的结果? –