views:

28

answers:

1

hello everybody!

i think this is a pretty easy question for you. I want to clear an attribute of a django-model.

If i have something like this:

class Book(models.Model):   
     name = models.TextField()  
     pages = models.IntegerField()  
     img = models.ImageField()

In an abstract function i want to clear an attribute, but at that time i don't know what type the field has. So examples would be name="", pages=0 or img=None.. Is there a way to do it in a generic way? I search something like SET_TO_EMPTY(book,"pages")

Do you know a function like that?

many thanks in advance!

A: 

I don't think there is a clean way of doing it. However, assuming you've set a default value (which you don't have) you can do it like this:

book.img = book._meta.get_field('img').default

Do note that your current model won't allow a None value. To allow those you have to set null=True and blank=True. For pages you need a default=0.

WoLpH