Thursday, July 5, 2012

Help using the clean() overwrite to control if user selects more than one boolean field in the admin

I am running into a problem, I asked for assistance in IRC but I still did not understand.

I have 2 objects one called Item, the other Upload, upload is linked to Item via Fkey and is being displayed inline. Upload also has a field called Featured. I am trying to override the clean() method for this so that user has to and can only select 1 upload item as featured. I tried reading the docs, but I am still confused can someone help me with this please? Here is my models.py and my admin.py

Model.py-----------------------------------------------------------------------------------

import datetime

from django.db import models
from django.contrib.auth.models import User


class Category(models.Model):
    label = models.CharField(max_length=128)
    description = models.TextField()
   
    user = models.ForeignKey(User)
   
    class Meta:
        verbose_name_plural="categories"
   
    def __unicode__(self):
        return self.label

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']
               
                       
    def __unicode__(self):
        return self.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/-/RLh8Fz97gcEJ.
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