You don't need to override choices for Model, because it is global to
validate "what values accepted to store into database". It's not depends on
current request scope or session.

If you need to provide user with subset of choices to choose from
dynamically, then you should use a Form for this.

class SomeModel(models.Model):
    somefield = models.CharField(max_length=100, choices=all_posible_values)
# or you can omit choices completely here

class SomeModelForm(forms.ModelForm)
    class Meta:
        model = SomeModel
        widgets = {'somefield': forms.Select}

    def __init__(self, *args, **kwargs):
        super(SomeModelForm, self).__init__(*args, **kwargs)
        self.fields['somefield'].widget.choices = somemethod()

Also you can write clean_somefield method to check that user didn't try to
mislead you.

On Thu, May 12, 2011 at 10:59 AM, Thomas Weholt <thomas.weh...@gmail.com>wrote:

> I got a model looking something like this
>
> class SomeModel(models.Model):
>    somefield = models.CharField(max_length=100, choices=somemethod())
>
> The problem is that somemethod, which produces the choices to give in
> the form in the admin, is called when the module is imported, not when
> the form instance is created. The result from the somemethod call is
> dynamic based on custom user defined code and not some static tuple or
> list.
>
> So my question is: how can I get the choices to be set at the time of
> form creation? Tried overriding the __init__, but then somefield was
> only available as a string and I could not set any choice-property.
>
> --
> Mvh/Best regards,
> Thomas Weholt
> http://www.weholt.org
>
> --
> 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