Thursday, July 5, 2012

Overriding the clean() method to an inlines model

Greetings, can someone point out how can I accomplish this:

I have 2 models Item and Upload, upload has an Fkey to Item and is being displayed Inlines. I am trying to override the clean() method so that only 1 upload object per item can be selected as featured. I asked the forums and was told the answer but still was not able to understand what I need to do. for simplicity sake lets just say I want to override the clean() method so that if any field or the upload is checked on it does not validate. Here is what my models.py and admin.py look like. Thanks in advance

Models.py------------------------------------------------------------------------------------------------------------------------------------
class Item(models.Model):
   
    title= models.CharField(max_length=64,
                           help_text="Lets name this item shall we? Maximum 128 chars")
    excerpt= models.CharField(max_length=384,
                           help_text="Small excerpt of your content")
    content= models.TextField()
   
    pub_date = models.DateTimeField(auto_now_add=True)
    visible_date= models.DateTimeField(default=datetime.datetime.now)

    slug = models.SlugField(unique=True)
    category = models.ForeignKey(Category)
   
   
    def __unicode__(self):
        return self.title
   
class Upload(models.Model):
   
    image = models.ImageField(blank=True, upload_to='images')
    title = models.CharField(max_length=100, blank=True)
    featured = models.BooleanField(default=False,blank=True)
    caption = models.CharField(max_length=256, blank=True)
   
    item = models.ForeignKey(Item)
   
    class Meta:
        ordering =['title']

admin.py---------------------------------------------------------------------------------------------------------------

from django.contrib import admin
from blogger.models import Category,Item, upload


class PhotoInline(admin.StackedInline):
    model = upload
   
    def clean_Upload(self):
        if upload.featured == True:
            raise forms.ValidationError("error")
        return self.cleaned_data

class ItemAdmin(admin.ModelAdmin):
    inlines =[PhotoInline]

admin.site.register(Category, )
admin.site.register(Item, ItemAdmin)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/4x32zvik7DQJ.
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