2012-01-31 73 views
1

我對Django對象模型有些困惑。我的模型是這樣的:關於Django的OneToOneField,ManyToManyField和ManyToOneField的說明

# Create your models here. 
class Item(models.Model): 
    code = models.CharField(max_length=200, unique=True) 
    barcode = models.CharField(max_length=300) 
    desc = models.CharField('Description',max_length=500) 
    reg_date = models.DateField('registered date') 
    registrar = models.CharField(max_length=100) 

    def __unicode__(self): 
     return self.code + ' : ' + self.desc 

class ItemInfo(models.Model): 
    item = models.OneToOneField(Item, primary_key=True) 
    supplier = models.ForeignKey(Supplier) 
    stock_on_hand = models.IntegerField() 
    stock_on_order = models.IntegerField() 
    cost = models.IntegerField() 
    price = models.IntegerField() 
    unit = models.CharField(max_length=100) 
    lead_time = models.IntegerField() 

但是,當我試圖檢索項目和ItemInfo到modelforms,我得到這個錯誤: 'ModelFormOptions' object has no attribute 'many_to_many'。我懷疑這條線supplier = models.ForeignKey(Supplier)有什麼問題。有人可以解釋我什麼時候使用ForeignKey或關係字段(OneToOneFields, ManyToManyFields, ManyToOneFields)

編輯:的ModelForm:

class ItemForm(ModelForm): 
    class Meta: 
     model = Item 
     widgets = { 
      'registrar' : TextInput(attrs={'ReadOnly' : 'True'}) 
     } 

class ItemInfoForm(ModelForm): 
    class Meta: 
     model = ItemInfo 
     exclude = ('item') 

我這是怎麼產生從模型填充值形式:

def details(request, code): 
    csrf_context = RequestContext(request) 
    current_user = User 
    if request.user.is_authenticated(): 
     item = Item.objects.get(pk=code) 
     item_info = ItemInfo.objects.get(pk=item.pk) 
     item_form = ItemForm(instance=item) 
     item_info_form = ItemInfoForm(instance=item_form) 
     return render_to_response('item/details.html', 
            {'item_form' : item_form, 'item_info_form' :  item_info_form}, 
            csrf_context) 
    else: 
     return render_to_response('error/requires_login.html', csrf_context) 

Traceback: 
Traceback: 
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response 
    111.       response = callback(request, *callback_args, **callback_kwargs) 
File "G:\tulip\stock\item\views.py" in details 
    131.   item_info_form = ItemInfoForm(instance=item_form) 
File "C:\Python27\lib\site-packages\django\forms\models.py" in __init__ 
    237.    object_data = model_to_dict(instance, opts.fields, opts.exclude) 
File "C:\Python27\lib\site-packages\django\forms\models.py" in model_to_dict 
    112.  for f in opts.fields + opts.many_to_many: 

Exception Type: AttributeError at /item/details/1/ 
Exception Value: 'ModelFormOptions' object has no attribute 'many_to_many' 
+0

你能後的追蹤?和你的ModelForm。我很好奇什麼試圖訪問'many_to_many' ... – 2012-01-31 08:07:50

+0

@YujiTomita這裏是模型和追蹤 – 2012-01-31 08:22:21

+0

@YujiTomita明白了!我通過表單作爲此行的參數item_info_form = ItemInfoForm(instance = item_form)'。應該是'item_info_form = ItemInfoForm(instance = item_info)'。有人請將其作爲答案發布,以便我可以打勾。得幫助像我這樣笨拙的爵士哈哈。 – 2012-01-31 08:42:42

回答

2

你正在使用ItemForm實例實例化ItemInfoForm。雖然instanceItemInfo實例,而不是形成

正確的路線:

item_info_form = ItemInfoForm(instance=item_info)