What does the Form side of a RelatedListField look like?

7 views
Skip to first unread message

pdohe...@gmail.com

unread,
Mar 14, 2019, 1:24:32 PM3/14/19
to djangae-users
I've been trying in vain to establish a RelatedListField relationship between two models, but can't seem to work out how to allow users to select the related entities from a Form instance. Unfortunately, every approach I've tried has resulted in an empty list or an exception which says, "'Foo' object is not iterable".

So, given the following relationship, how should I create/populate the MultipleChoiceField in my Form instance? (I'll eventually need to filter the available options, but will revisit once I have the baseline version working.)

class Foo(models.Model):
    name = models.CharField(
        max_length=70,
        unique=True
    )

class Bar(models.Model):
    foos 
= fields.RelatedListField('Foo')

Adam Alton

unread,
Mar 14, 2019, 1:37:43 PM3/14/19
to pdohe...@gmail.com, djangae-users
I think that this should "just work" if you create a standard Django model form for the Bar model.

The RelatedListField has a custom 'formfield' method, so if you use a Django model field then Django should call that, which should give you a suitable form field (in this case it should be giving you Djangae's custom OrderedModelMultipleChoiceField).

See:
and 

Purely from looking at the Djangae code, this has clearly been done before (hence that code existing).  So if it doesn't work then there's something unexpected happening (whether it's in Djangae, Django or your own code).

What happens when you create a standard Django model form for the model?
Adam


--
You received this message because you are subscribed to the Google Groups "djangae-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to djangae-user...@googlegroups.com.
To post to this group, send email to djanga...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/djangae-users/caed953f-c49b-4d95-b870-c8e51b15ccef%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

pdohe...@gmail.com

unread,
Mar 14, 2019, 2:27:19 PM3/14/19
to djangae-users
Thanks for the quick response and the links, Adam!

Per your suggestion, I was able to get a POC working by explicitly using `OrderedModelMultipleChoiceField` in my Form (e.g. `foos = OrderedModelMultipleChoiceField(Foo.objects.all())`).

Thank you for the nudge and hopefully this exchange is useful to others in the future.

Now on to figuring out how to make the labels comprehensible (they're currently "Foo object") and filter the initial list ...

Adam Alton

unread,
Mar 14, 2019, 2:45:36 PM3/14/19
to pdohe...@gmail.com, djangae-users
Per your suggestion, I was able to get a POC working by explicitly using `OrderedModelMultipleChoiceField` in my Form (e.g. `foos = OrderedModelMultipleChoiceField(Foo.objects.all())`).
Did you try just using a standard model form, specifying the `model` and the `fields` in the `Meta` class but letting Django automatically generate the actual fields?  If so, and that didn't work, then that seems like a bug.

Now on to figuring out how to make the labels comprehensible (they're currently "Foo object")
The simplest way is to just override the __unicode__ method on the Foo model.

and filter the initial list ... 
Have you tried limit_choices_to


pdohe...@gmail.com

unread,
Mar 14, 2019, 3:05:28 PM3/14/19
to djangae-users
> Did you try just using a standard model form, specifying the `model` and the `fields` in the `Meta` class but letting Django automatically generate the actual fields?  If so, and that didn't work, then that seems like a bug.

I haven't tried that. I don't have time to test this right now, but I will make a note and revisit this approach when I have some time.

> he simplest way is to just override the __unicode__ method on the Foo model.

This is good to know. Though, this time around I subclassed `OrderedModelMultipleChoiceField` and overrode `label_from_instance`.

> Have you tried limit_choices_to

Unfortunately, I don't think that's an option because the value I'm filtering by is dynamic. I was able to wire things up by overriding the Form's `__init__` and passing a kwarg.

Thanks, again, for your help and for continuing to support such a useful tool!

Adam Alton

unread,
Mar 15, 2019, 7:34:46 AM3/15/19
to pdohe...@gmail.com, djangae-users
👍 Glad to be of help.

Reply all
Reply to author
Forward
0 new messages