About Blog Gallery Twit

Archive for June 01, 2008

User Interfaces

I watched an interesting video/podcast on User Interface development over on dotnetrocks TV. If you do some UI development or are interested in it, I would highly recommend viewing this show:
The Science of a Great User Experience Part 1.

Model Inheritance

I’m working on several projects in my spare time, all of them probably will never see the light of day. That’s OK, I mainly work on these to help me keep up with what’s going on outside of the box I live in. Anyways, I was interested in creating some defaults to all of my models on a project where there has to be a strong level of auditing. On the models themselves I want to be able to store some basic information of create user, create date, update user and update date. Seems pretty normal, but there’s a ton of models in one of my applications, so I didn’t want to type these things over and over and over again.

class ModelDefaults(models.Model):
    created_by = models.ForeignKey(User, related_name="%(class)s_created_by")
    created_at = models.DateTimeField()
    updated_by = models.ForeignKey(User, related_name="%(class)s_updated_by")
    updated_at = models.DateTimeField()

    def save(self):
        if self.id == None:
            self.created_at = datetime.datetime.now()
            self.created_by = user_id
            self.updated_at = datetime.datetime.now()
            self.updated_by = user_id
        else:
            self.updated_at = datetime.datetime.now()
            self.updated_by = user_id

        super(ModelDefaults, self).save()

    class Meta:
        abstract = True

This is my ModelDefaults, as you can see. All of the models that use this are written as such:

class MyNewModel(ModelDefaults):
    name = ...

I haven’t really tested it, but I’m assuming that it will work. I’m thinking of what else I want to store in here, but I’ve already hacked an object audit log similar to the Admin pages auditing. That too has to be tested, as this app should really only be used from outside the Admin pages, including the admins of the application. Interesting stuff, maybe if I ever get a full app complete I’ll light it up on here for fun.