Saturday, January 26, 2013

Re: Form with variable fields

Le dimanche 27 janvier 2013 00:57:42 UTC+1, arnonym a écrit :
On Sat, 26 Jan 2013 11:39:20 -0800 (PST) sephii
<sylvaint...@gmail.com> wrote:
> Hey there,
>
> I'm trying to create a form with a "static" part (a "title" field, a
> "date" field) and a variable part (these are "artist names", so
> that's a single field that can be repeated multiple times, with a
> minimum of 1). I first thought about the MultiValueField but it
> requires several fields, and in my case I only want to use 1 field,
> just like an inline with one field in the Django admin. I then tried
> to go with a Formset, but I wasn't able to set it as mandatory (even
> if the field is marked as mandatory, if it's left blank it's just
> like the form is not filled so the validation always passes). Here's
> the code I tried for the formset part:

When one entry in one table/model has several assorted entries of an
independent type, thats called a 1-to-n-relation. Or a OneToManyField
in django.

So essentially when you have a list of CDs, they all have album,
title and stuff. But then you have a list of artists and each of these
entries belongs to one CD while one CD can have several artist entries.
OneToManyField from the artist to the CD.
But one artist can be on several CDs. So actually you want to look at
the ManyToManyField, because one artist can be on several CDs and
several artists can be on one CD. Thats an n-to-m relation.

Once you get these relations right, the django forms do the rest for
you.

Maybe that helps,

Arnold

Hi Arnold,

Thanks for your answer. I wanted to avoid using a ManyToMany field in my case for two reasons:

1. The artist table has about 700'000 entries, which is really too big to fit in a <select> element
2. I could create an autocomplete text widget to overcome point 1), but the user should also be able to set a new artist that doesn't exist yet in the database using these fields

Feel free to let me know if you think of a solution that would overcome these 2 issues.

Cheers

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment