Tuesday, September 1, 2015

Re: two modelForm fields to a one model Field

On Tue, Sep 1, 2015 at 1:13 PM, Leandro Severino
<leandro@professionalit.com.br> wrote:
> Hi,
>
> In my first post, I have a simple question:
>
> Its possible a modelForm have two fields for a one model Field ?
>
> My codes:
>
>
> In my models:
>
> class MyClass(models.Model):
> COMMON_AMENITIES_CHOICES = (('A', 'Aditional'),
> ('C', 'Commom'))
>
> type = models.CharField(u'type', max_length=1,
> choices=COMMON_AMENITIES_CHOICES )
>
> name = moldes.CharField(max_length=250)
>
>
>
> With a default form I have this:
>
> class MyClassForm(forms.Form):
> from .models import MyClass
>
> COMMON_AMENITIES_CHOICES = tuple(Amenitie.objects.filter(
> type_amenitie='C').values_list('id', 'name'))
> ADITIONAL_AMENITIES_CHOICES = tuple(Amenitie.objects.filter(
> type_amenitie='A').values_list('id', 'name'))
>
> common_amenitie = forms.MultipleChoiceField(
> choices=COMMON_AMENITIES_CHOICES,
> label=u'Common Amenities'
> )
> aditional_amenitie = forms.MultipleChoiceField(
> choices=ADITIONAL_AMENITIES_CHOICES,
> label=u'Common Amenities'
> )
>
>
>
> My question:
>
>
> Its possible implements this with a modelForm?


Yes, although your model does not appear to be setup in an ideal
manner, which is probably why your form is not coming out the way you
intended. You have no way to store what values the user would pick
even if the form was correct.

You probably need to set up something like this:


class MyClass(models.Model):
COMMON_AMENITIES_CHOICES = (('A', 'Aditional'), ('C', 'Commom'))

type = models.CharField(u'type', max_length=1,
choices=COMMON_AMENITIES_CHOICES )

name = moldes.CharField(max_length=250)

common_amenities = models.ManyToManyField(Amenitie,
limit_choices_to={'type_amenitie': 'C'},
related_name='myclass_common_set')
additional_amenities = models.ManyToManyField(Amenitie,
limit_choices_to={'type_amenitie': 'A'},
related_name='myclass_additional_set')


See here for the reference for the ManyToManyField options:
https://docs.djangoproject.com/en/1.8/ref/models/fields/#manytomanyfield

At this point, you may not even need to define a model form (if you
are using Class Based Views). The one generated should be close to the
regular form you outlined above, although you might if you need
additional validation (ie disallow picking additional amenities
without picking common amenities, etc.) or if you need to override the
widgets used in the form rendering.

-James

--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciUXG8p2fKxbVOJp69i7A_gOo6J_5o9vAb7QUvng6ThTFQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment