我正在尝试使用formset
在我的Neo4j节点之间创建连接。这些模型使用django-neomodel包构建。它们与使用外键无关,但由于不是 inline formset
这不重要,对吗?
models.py
class Person(DjangoNode):
uid = UniqueIdProperty()
name = StringProperty(max_length=50)
created_at = DateTimeProperty(default=datetime.now)
friends = RelationshipTo('Friend', 'FRIENDED', model=FriendRel)
# DjangoNode class must have a Meta class and documentation specifies 'django_node'
class Meta:
app_label = 'django_node'
class FriendRel(DjangoRel):
created_at = DateTimeProperty(default=datetime.now)
class Meta:
app_label = 'django_rel'
forms.py
class PersonForm(forms.ModelForm):
class Meta:
model = Person
# don't need fields for automatically assigned keys like `uid` and `created_at`
fields = ['name']
class FriendRelForm(forms.ModelForm):
class Meta:
model = FriendRel
exclude = () #"creating ModelForm without either 'fields' || 'exclude' attribute is prohibited"
FriendRelFormSet = formset_factory(FriendRelForm, extra=1)
form.html
<div>
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<table class="table">
{{ friends.management_form }}
{% for form in friends.forms %}
{% if forloop.first %}
<thead>
<tr>
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}</th>
{% endfor %}
</tr>
</thead>
{% endif %}
<tr class="{% cycle "row1" "row2" %} formset_row">
{% for field in form.visible_fields %}
<td>
<!-- Include the hidden fields in the form -->
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
<input type="submit" value="Save"/>
</form>
</div>
我期待一个“朋友”formset出现在渲染表单中,但我不太确定如何将它放到那里。如果我添加以下内容,则会收到错误消息:
class FriendRelForm(forms.ModelForm):
class Meta:
model = FriendRel
exclude = ()
fields = ['friends']
***ERROR*** django.core.exceptions.FieldError: Unknown field(s) (friends) specified for FriendRel