具有两个相同外键的Django Inlineformset

迪安·克里斯蒂安·阿玛达(Dean Christian Armada)

以下是我执行inlineformset django所需的代码。问题是我有两个具有相同ForeignKeys的属性,并且它返回Django错误

models.py

class Reference(models.Model):
    user = models.ForeignKey(UserProfile, related_name="user_mariner", default=None)
    verified_by = models.ForeignKey(UserProfile, related_name="verified_by", default=None)
    company = models.ForeignKey(Company, default=None)
    date = models.DateField(null=True, blank=True, default=None)
    person_contacted = models.ForeignKey(PersonReference, default=None)
    veracity_seagoing_history = models.NullBooleanField()
    health_problem = models.NullBooleanField()
    financial_liability = models.NullBooleanField()
    rehiring_prospects = models.NullBooleanField()
    character = models.TextField(null=True, blank=True, default=None)
    comments = models.TextField(null=True, blank=True, default=None)

表格

class ReferenceForm(forms.ModelForm):
    class Meta:
        model = Reference
        exclude = '__all__'

views.py

try:
    reference = Reference.objects.filter(user=id)
    ReferenceFormSet = inlineformset_factory(UserProfile, Reference, fields='__all__', extra=3, can_delete=True )
    reference_form = ReferenceFormSet(request.POST or None, instance=user_profile)
except:
        print "%s - %s" % (sys.exc_info()[0], sys.exc_info()[1])

template = "application-profile/profile.html"
context_dict = {}
context_dict['reference_form'] = reference_form
return render(request, template, context_dict)

profile.html

{{ reference_form }}

错误

<type 'exceptions.ValueError'> - 'mariners_profile.Reference' has more than one ForeignKey to 'login.UserProfile'.
suselrd

您必须指定要用作父级“链接”的字段名称。就像是:

ReferenceFormSet = inlineformset_factory(UserProfile, Reference, fk_name='user', fields='__all__', extra=3, can_delete=True )

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在具有类似ID的Spring Boot中联接两个没有外键的表

Lua表-具有相同键的两个条目

__init __()得到了一个意外的关键字参数'instance',具有针对inlineformset_factory的自定义格式集

用于同时编辑具有外键关系的两个Django模型的表单

两个孩子在React中具有相同的键

Django-修改Inlineformset删除按钮

如何从Django中具有外键关系的两个模型中选择值

遇到两个孩子具有相同的键错误

如何使用manyToMany和inlineformset表示Django中的家庭关系?

Django:两个模型的外键

在Django inlineformset_factory中更改图像会将其置于列表的末尾

向Django InlineFormSet添加额外的字段

如何为具有相同关系的两个外键的模型编写关联?

使用两个提交按钮将Django inlineformset保存为草稿

加入两个相同的外键

MySQL数据库在一个表中具有两个外键

Django-具有两个以上外键的模型

在具有两个外键(php)的一个表中插入数据

Django inlineformset不保存ForeignKey字段

django inlineformset,如何一次只允许1个布尔字段为true?

在Django中使用inlineformset_factory时将模型用户设置为当前用户

具有两个外键的两个表之间的Laravel关系

禁用表单字段 Django inlineformset_factory

'function' 对象没有属性 '_meta' inlineformset_factory django

django-inlineformset-crispy-如何显示non_field_errors

为什么 django inlineformset_factory 数据没有保存在基于类的更新视图中?

Django:使用 inlineformset_factory 更新和创建数据记录

Django CreateView 结合 modelformset 和 inlineformset

Django CreateView 结合 modelformset 和 inlineformset