用外键在django模型表单中创建对象

时间:2011-04-28 06:55:20

标签: django-forms foreign-keys

我在我的模型表单中插入一条新记录,其中我的模型是包含foriegn键的子表单。当我提交表单时,它会给出错误,它应该是foriegn键的实例。

这是我的模特

class MMEditidState(models.Model):
  state_id = models.IntegerField(primary_key = True)
  state_dremelid = models.ForeignKey(MMDremelDump, db_column = 'state_dremelid')
  assignee = models.CharField(max_length = 50)
  state = models.CharField(max_length = 50)
  role = models.CharField(max_length = 50)
  date = models.DateTimeField() 
  class Meta:
    db_table = u'mm_editid_state'
  def __unicode__(self):
    return u'%s %s' % (self.state_dremelid, self.assignee)

  class MMEditidErrors(models.Model):
     error_id = models.IntegerField(primary_key = True)
     error_stateid = models.ForeignKey(MMEditidState, db_column = 'error_stateid')
     feature_type = models.CharField(max_length = 20)
     error_type = models.CharField(max_length = 20)
     error_nature = models.CharField(max_length = 50, null = True)
     error_details = models.CharField(max_length = 50)
     error_subtype = models.CharField(max_length = 200)
     date = models.DateTimeField()
  class Meta:
     db_table = u'mm_editid_errors'
  def __str__(self):
     return "%s" % (self.error_dremelid)
  def __unicode__(self):
     return u'%s' % (self.error_dremelid)

这是我的观点

def qcthisedit(request, get_id):
  if request.method == "POST":
     form = forms.MMEditidErrorForm(get_id, request.POST)
     if form.is_valid():
        form.save()
     return http.HttpResponseRedirect('/mmqc/dremel_list/')
  else:
    form = forms.MMEditidErrorForm(get_id)
    return shortcuts.render_to_response('qcthisedit.html',locals(),
                                  context_instance = context.RequestContext(request))

这是我的表格

class MMEditidErrorForm(forms.ModelForm):
   def __init__(self,get_id, *args, **kwargs):
     super(MMEditidErrorForm, self).__init__(*args, **kwargs)
     dremel = MMEditidState.objects.filter(pk=get_id).values('state_id')
     dremelid = int(dremel[0]['state_id'])
     self.fields['error_stateid'] = forms.IntegerField(initial = dremelid, 
                                                   widget = forms.TextInput(
                                                              attrs{'readonly':'readonly'}))
     feature_type  = forms.TypedChoiceField(choices = formfields.FeatureType)
     error_type  = forms.TypedChoiceField(choices = formfields.ErrorType)
     error_nature = forms.TypedChoiceField(choices = formfields.ErrorNature)
     error_details = forms.TypedChoiceField(choices = formfields.ErrorDetails)
     error_subtype = forms.TypedChoiceField(choices = formfields.ErrorSubType)
     class Meta:
        model = models.MMEditidErrors
        exclude = ('error_id','date')  

当我提交表单时,我收到错误

Cannot assign "1": "MMEditidErrors.error_stateid" must be a "MMEditidState" instance.

所以我添加了行

get_id = MMEditidState.objects.get(pk = get_id)

现在我收到了下面提到的错误

int() argument must be a string or a number, not 'MMEditidState'
form = forms.MMEditidErrorForm(get_id, request.POST)

中的

有人可以帮忙吗

由于 维克拉姆

1 个答案:

答案 0 :(得分:1)

我通过简单地使用自定义表单而不是模型表单解决了这个问题。在将数据存储在数据库中时,我在views.py

中管理自己