views:

55

answers:

2

I have a model, which has several "automatic" fields, like this:

class Message(Model):
    subject = CharField(max_length = 200)
    message = TextField()
    created = DateTimeField()
    last_status_change = DateTimeField()
    status = CharField(max_length = 10)

In my database (Postgres) I set up default values for created, last_status_change, status also, I set up a trigger to update last_status_change every time status changes.

However, when I try to create and save a model instance like this:

m = Message(subject = 'Hello', message = 'Long and boring day')
m.save()

Django tries to insert NULLs into those missing fields, which is wrong. Is there a way to make it emit insert without those fields or do I have to duplicate my DB functionality in the code?

Thanks!

A: 

I'd recommend you to put default values into model fields definition and implement special method for changing status or rewrite save() method instead of database level triggers.

Vladimir Prudnikov
+1  A: 

you can try setting blank to true .like below......

class Message(Model):
    subject = CharField(max_length = 200)
    message = TextField(blank=True,null=True)
    created = DateTimeField(auto_now=True)
    last_status_change = DateTimeField(auto_now=True)
    status = CharField(max_length = 10,blank=True,null=True)

    def save(self):
         self.last_status_change=Datetime.now()


mossplix
Blank tries to set status to empty string, which is invalid status. From what I see, there is just no way to do what I want in django...
maksymko
try and add a method in the model which takes self as the first argument and manupulates the status the way you want it.
mossplix