Hello!
I have a problem which i can't solve on my own. I have already
searched the web, but without success.
I have the following two models which are related with a many to many
relationship using Django 1.1 with Postgres on Unix:
FIELD_TYPES = ( (u'int',u'int'),
(u'string',u'string'),
)
class MediaAttribute(models.Model):
attribute = models.CharField(max_length=100)
value = models.CharField(max_length=100)
type = models.CharField(max_length=30, choices=FIELD_TYPES,
default='string')
def __str__(self):
return self.attribute + "=" + self.value
class MediaItem(models.Model): # inspired by the ImageModel of
photologue
id = models.AutoField(primary_key=True)
file = models.FileField(upload_to='uploads/archive/')
view_count = models.PositiveIntegerField(default=0,
editable=False)
created_at = models.DateField(auto_now=True)
updated_at = models.DateField(auto_now=True)
attributes = models.ManyToManyField(MediaAttribute,blank=True)
def __str__(self):
return self.file.name
def
create_or_update_attribute(self,the_attribute,the_value,the_type='string'):
'''
Updates or adds an attribute of a mediaItem.
'''
related_attribute =
self.attributes.filter(attribute=the_attribute,value=the_value)
if related_attribute:
return
related_attribute =
self.attributes.filter(attribute=the_attribute)
if related_attribute.count() is not 0:
for attribute in related_attribute:
self.attributes.remove(attribute)
MediaAttribute_object, created =
MediaAttribute.objects.get_or_create(attribute=the_attribute,value=the_value,type=the_type)
self.attributes.add(MediaAttribute_object)
self.low_level_save()
return MediaAttribute_object
def save(self, *args, **kwargs):
super(MediaItem, self).save(*args, **kwargs)
current_photgrapher = 'photographer name'
self.create_or_update_attribute('photographer',
current_photgrapher)
I want to associate keywords with a mediaitem, but in a loose and
dynamic way since many photos share the same attributes as
photographer for example. i want to save the photographer's name at
the beginning after the mediaItem object has been created.
The strange thing is, that when i call the function
create_or_update_attribute from within save(). There is no error
message and also a new MediaAttribute is properly created, but it's
not connected with the MediaAttribute in the database.
The function "create_or_update_attribute" works correct when called
outside the save function (e.g. from a view). Then a connection
between the MediaItem and the MediaAttribute is made correctly. Like
so:
m,c = MediaItem.objects.get_or_create(pk=1)
m.create_or_update_attribute('photographer','test')
(this works correct. it creates a new MediaAttribue and makes also a
connection to the MediaAttribue in the Database.)
Can anybody help me? I would appreciate any advice!
Thank you very much,
Frederic
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
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.