2012-01-31 105 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)