Changes between Initial Version and Version 1 of Ticket #18168, comment 6


Ignore:
Timestamp:
Aug 3, 2012, 5:46:15 AM (12 years ago)
Author:
Joeri Bekker

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #18168, comment 6

    initial v1  
    1 This would limit the flexibility of the {{{ formfield_for_choice_field }}} function quite a bit, which I find unfortunate.
     1The function name {{{ formfield_for_choice_field }}} suggests that you are changing the form field, not the database field which is exactly what happens. The form field choices are updated, the model field choices are not, hence validation (on the model level) fails.
    22
    3 Adding arbitrary values to the choices is not wrong, since they are added to the "choices" and therefore not incorrect as a choice. It would be different if you change choices through some Javascript and let Django accept it.
     3If you want flexible choices, define a {{{ CharField }}} in your model without any {{{ choices }}} argument. Create a form where you overwrite the widget for that {{{ CharField }}} with a {{{ forms.Select }}}. Then use the {{{ formfield_for_dbfield }}} function (not the {{{ formfield_for_choice_field }}} since it's not a choice field in the database model) and fill the {{{ kwargs['widget'].choices }}} with your list of choices.
    44
    5 > I don't think it makes sense to allow in the form values that aren't allowed by the model
     5Example:
     6{{{
     7# admin.py
    68
    7 The choice fields (often on top of database !CharField or !IntegerField) do no enforce any referential integrity anyway. One can simply remove one of the choices in the code and the database nor the Django admin (for reading at least) would complain, which is the correct behavior.
     9from django import forms
     10from django.contrib import admin
     11from .models import Variable
    812
    9 As a workaround, you overriding {{{ YourForm.__init___ }}} and use your custom form in the {{{ ModelAdmin }}} which adjusts your choices (depends a bit on what you need to base your choices on).
     13class VariableForm(forms.ModelForm):
     14    class Meta:
     15        model = Variable
     16        widgets = {
     17            'name': forms.Select(),
     18        }
     19
     20class VariableAdmin(admin.ModelAdmin):
     21    form = VariableForm
     22
     23    def formfield_for_dbfield(self, db_field, **kwargs):
     24        if db_field.name == "name":
     25            kwargs['widget'].choices = (
     26                ('quux', 'quux'),
     27                ('quuux', 'quuux'),
     28            )
     29        return super(VariableAdmin, self).formfield_for_dbfield(db_field, **kwargs)
     30
     31admin.site.register(Variable, VariableAdmin)
     32}}}
Back to Top