使用外键以 django 模型形式创建对象

发布于 2024-11-03 22:49:30 字数 3213 浏览 1 评论 0原文

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

这是我的模型

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) 中 )

有人可以帮忙解决这个问题吗

谢谢 维克拉姆

I am inserting a new record in my model form where my model is child form containing a foriegn key. When I am submitting the form it give error it should be instance of the foriegn key.

Here is my model

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)

Here is my View

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))

Here is my form

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')  

When I submit the form I am getting the error

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

So I have added line

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

Now I am getting the below mentioned error

int() argument must be a string or a number, not 'MMEditidState'

in form = forms.MMEditidErrorForm(get_id, request.POST)

Can someone help on this

Thanks
Vikram

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独孤求败 2024-11-10 22:49:30

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

I have solved this problem by simply using the custom forms instead of model forms. While storing the data in the database, I managed myself in the views.py

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文