Thanks for the pointer! I finally ended up defining my own field, which allowed me more flexibility:
class CodeCategoryChoiceField(ChoiceField): def clean(self, value): return CodeCategory.objects.get(pk=value) Thanks! Julien On Mar 27, 8:53 pm, peschler <[EMAIL PROTECTED]> wrote: > Not quite sure, but IMHO you need to use a ModelChoiceField instead of > a ChoiceField in your formfield_for_dbfield(). > > http://www.djangoproject.com/documentation/newforms/#fields-which-han... > > pe > > On 27 Mrz., 06:21, Julien <[EMAIL PROTECTED]> wrote: > > > 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"> |- Best > > Practices</option> > > > <option > > value="13"> |- > > Collaboration</option> > > > ..... > > > <option > > value="15"> |- > > 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 > Problems/Challenges > Creativity</ > > option> > > > ... > > > <option value="3">All > Problems/Challenges > Communication</ > > option> > > <option value="4">All > Problems/Challenges > 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 -~----------~----~----~----~------~----~------~--~---