In the code snip below, I am overriding the save() method of my
"Project" object so that I can record the modification to that project
by creating and saving a "ProjectModification" object.

The idea is simple, but note that I have to convert the project field
values to strings before comparing them for inequality:

   if (str(self.__dict__[field]) !=
str(old_project.__dict__[field]) ) :

The reason I am doing this is because the type of self and old_project
turn out to be different.  It seems that regardless of the actual type
of old_project, it is regarded as type 'unicode'.  Interestingly, this
isn't the case when testing this same bit of code from the "django
shell", only when testing from the django server.

My first question is: Can anyone explain this phenomenon?
My second question is: Is there a way to resolve the actual type of
old_project.__dict__[field] after loading it here
p=Project.objects.filter(id=self.id)[0]... I think this would be
preferable to comparing all values as strings.

Many thanks,
Adam


    def record_project_modifications(self, old_project):
    '''Creates and stores ProjectModifications that differ between
this project and old_project.'''
        fields = [f.attname for f in
Project.objects.model._meta.fields]
        for field in fields:
            if (str(self.__dict__[field]) !=
str(old_project.__dict__[field]) ) :
                pmod = ProjectModification()
                pmod.user_id = 21
                pmod.project = self
                pmod.date    = datetime.datetime.now()
                pmod.field   = field
                pmod.value   = self.__dict__[field]
                pmod.save(pmod)

    def save(self):
    '''Override models.Model.save so we can call
record_project_modificationsbefore saving the model'''
        p = Project.objects.filter(id=self.id)[0]
        self.record_project_modifications(old_project=p)
        print "saving..."
        models.Model.save(self)
        print "saved"
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to