Hi,

I was busy doing other things, and then I got back to that problem. I
spent another couple of hours on it and I still don't get it... :(

My custom widget displays correctly in the admin form and generates
something like the following code:

<label for="id_category" class="required">Category:</label>
<select name="category" id="id_category">
<option value="14">All</option>
<option value="6">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|-&nbsp;Best
Practices</option>

<option
value="13">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|-
&nbsp;Collaboration</option>

.....

<option
value="15">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|-
&nbsp;Communication</option>
</select>


Note: All the "nbsp" are there for aligning the options properly.

But if I select an option and click "save", the 'category' value is
not saved, and NULL is recorded in the database instead. If I turn my
custom widget off and use the default widget, here's what the
generated code looks like:

<label for="id_category">Category:</label>
<select name="category" id="id_category">
<option value="" selected="selected">---------</option>
<option value="1">All &gt; Problems/Challenges &gt; Creativity</
option>

...

<option value="3">All &gt; Problems/Challenges &gt; Communication</
option>
<option value="4">All &gt; Problems/Challenges &gt; Collaboration</
option>
<option value="14">All</option>
</select>

To me, both pieces of code look fairly similar. In my custom widget,
only the order of appearance, and the name of options change, the id's
are the same. I don't understand why the value is not saved properly
when using my custom widget.. I've made custom widgets with newforms-
admin in the past and it always worked fine.
Do you have an idea of what I could be missing this time?

Thanks a lot!

Julien

On Mar 20, 5:02 pm, Julien <[EMAIL PROTECTED]> wrote:
> Hi there,
>
> Here's my model, whose admin widget is customized:
>
> class Code(models.Model):
>     name = models.CharField(max_length=100)
>     category = models.ForeignKey(CodeCategory, related_name='codes',
> null=True, blank=True)
>
> class CodeOptions(admin.ModelAdmin):
>
>     def formfield_for_dbfield(self, db_field, **kwargs):
>         if db_field.name == 'category':
>             choices = get_categories()
>             if 'initial' in kwargs:
>                 return form.ChoiceField(initial=kwargs['initial'],
> choices=choices)
>             else:
>                 return form.ChoiceField(choices=choices)
>         else:
>             return
> super(CodeOptions,self).formfield_for_dbfield(db_field,**kwargs)
>
> It works fine, in the sense that the widget displays correctly when
> editing or adding a Code object.
> However, if I click "Save", I get no error but NULL is stored in the
> 'category' attribute of the Code object.
>
> I really don't understand what's the problem. Do you have an idea?
>
> Thank you!
>
> Julien
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to