Sunday, October 2, 2011

Re: Can model subclass change field options?

Hi Jonas,

You can override validation by adding a "clean" method to the ModelAdmin subclass. Look here for more details, https://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other.
That is what I have been doing. It might be hacky, someone please let me know if there is a better solution.


Going back to my earlier example it should look something like this. In the clean function you can also raise ValidationErrors to send back a custom error message.

# admin.py
from django.contrib import admin
from django.forms import widgets

class myModelAdmin(admin.ModelAdmin):
  title = CharField(widget=widgets.Select(choices=('mr','mrs','miss'))

  # This method name is dynamicly create using 'clean_' + model name to create
  # a method name. 
  def clean_title(self):
     data = self.cleaned_data['title']
     if(data == ''):
       raise ValidationError("you forgot to set the title")


  def clean(self):
    # grab already cleaned data so we can manipulate it
    data = super(myModelAdmin, self).clean()
    new_title = data['title']

    # do something to the old title then add it back to data
    data['title'] = new_title
    
    return data


admin.site.register(myModel, myModelAdmin)
Thats how I have been handling my validation issues. I hope this helps you figure out your validation issues. If you are still having trouble post the error message your are gettting, I might be able to help you more from that.

-Brian

Also if you have checked out DjangoSnippets you should check it out. I have found some great working examples on that site.


On Sun, Oct 2, 2011 at 4:04 PM, Jonas Cleve <cleve.jonas@googlemail.com> wrote:
Hi Brian,

that works so far for displaying the correct choices in the admin
interface. But the problem now is the validation. He always tells me
that my choice is not a correct value.
I have been looking for a way to change validation but I can't figure
out how to do this...

Thanks for your help so far.

Jonas

Am 02.10.2011 19:41, schrieb Brian Mehrman:
> HI, Artemis,
>
> If I understand you correctly you want to use a Select Widget for your
> CharField. And feed your choices to the select widget.
>
> In your admin.py file of your app you will want to override the model's
> form field.
>
>
> # models.py
> from django.db import models
>
> myModel(models.Model):
>   person = models.CharField()
>   title = models.CharField()
>
> # admin.py
> from django.contrib import admin
> from django.forms import widgets
>
> myModelAdmin(admin.ModelAdmin):
>   title = CharField(widget=widgets.Select(choices=('mr','mrs','miss'))
>
> admin.site.register(myModel, myModelAdmin)
>
> This code hasnt been tested but should be the route you want to take.
> You can find more info on widgets
> here(https://docs.djangoproject.com/en/1.3/ref/forms/widgets/). I hope
> this helps you.
>
> -Brian
>
> On Sun, Oct 2, 2011 at 12:20 PM, Artemis <cleve.jonas@googlemail.com
> <mailto:cleve.jonas@googlemail.com>> wrote:
>
>     Hi,
>
>     I have an model which contains a CharField.
>     Now I want to have different subclasses of this model each one with
>     different *choices* for the CharFiel. How can I implement this?
>
>     --
>     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
>     <mailto:django-users@googlegroups.com>.
>     To unsubscribe from this group, send email to
>     django-users+unsubscribe@googlegroups.com
>     <mailto:django-users%2Bunsubscribe@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+unsubscribe@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+unsubscribe@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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment