Sunday, June 17, 2018

Django formset for models without foreign key

I'm trying to use formset to create connections between my Neo4j nodes. These models have been constructed using the django-neomodel package. They aren't related using foreign keys, but since this isn't an inline formset that shouldn't matter, right?


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>



I'm expecting a "friend" formset to appear in the rendered form, but am not quite sure how to get it there. If I add the following I get an error:

  class FriendRelForm(forms.ModelForm):      class Meta:         model = FriendRel        exclude = ()        fields = ['friends']    ***ERROR*** django.core.exceptions.FieldError: Unknown field(s) (friends) specified for FriendRel

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3e387c8a-522b-4278-8784-aff15f0d9602%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment