Disclaimer - I have never done this before!

>From what I see I reckon you will need to create both a custom field and a
custom widget!

I think so because if you have TextInputs validation may fail on the
ModelMultipleChoiceField?
I would think you need to override the methods: __init__, clean, to_python
and compress.

I think you will also need to create a more customized widget inheriting
directly from MultiWidget?
I think you would need to override the methods: __init__, render, decompress

But I'm no expert ;-(


On Sun, Aug 26, 2012 at 7:42 PM, Nicolas Emiliani <or3s...@gmail.com> wrote:

> Hi,
>
> I've been struggling with this for the past two weeks, and i think it's
> time to ask.
>
> I have and admin form that has a ModelMultipleChoiceField that is used to
> display
> a ManyToMany model field that specifies a through model
>
> class Person(models.Model):
>     name = models.CharField(max_length=10)
>     attributes = models.ManyToManyField('Attribute',through='Has',
>                                         blank=True, null=True)
>
> class Has(models.Model):
>     person = models.ForeignKey('Person')
>     attribute = models.ForeignKey('Attribute')
>     value = models.CharField(max_length=10)
>
>
> class Attribute(models.Model):
>     name = models.CharField(max_length=10)
>     atype = models.CharField(max_length=2,
>                                           choices=(('TF','true/false'),
>                                                        ('IN','text')))
>
>
> class PersonAdminForm(forms.ModelForm):
>
>     class Meta:
>         model = Person
>
>     attrs = forms.ModelMultipleChoiceField( \
>                     label='Attributes',
>                     queryset=Attribute.objects.all(),
>                     widget=forms.CheckboxSelectMultiple(),
>                     )
>
>
> This CheckboxSelectMultiple works fine, but I want to be able to render
> each relation according to the Attribute.atype value, meaning if it is TF,
> then tender it as a CheckBox, if it is IN the render it as an InputText
>
> I inherited from CheckboxSelectMultiple so it does that, but then when I'm
> on the admin form and for one specific attribute of type IN, rendered as a
> TextInput,  I set a value (let's say 500) and then press save , the method
>
> SelectMultiple.value_from_datadict gets called and passed as data :
>
> <QueryDict: {u'csrfmiddlewaretoken':
> [u'GWLnuoTSTjjLmpYOizodBA4VA1FmwdQw'],
> u'_continue': [u'Save and continue editing'], u'name': [u'nico'],
> u'attrs': [ u'2']}>
>
> And this is what has been driving me nuts!
>
> u'attrs': [ u'2'],
>
> As you can see it only passes the id of the attribute, not the id and the
> value. What I
> need is something like
>
> u'attrs': [ (u'2','500')],
>
> Any ideas on how to fix this ?
>
> Thanks!
>
> --
> Nicolas Emiliani
>
> Lo unico instantaneo en la vida es el cafe, y es bien feo.
>
> --
> 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+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to