On Thursday, 2 January 2014 21:09:22 UTC, arthur...@gmail.com wrote:
-- I have the following model in my app, using the content-type django framework in version 1.6.1 :class GenericMedia(models.Model): limit = models.Q(model = 'Image') | models.Q(model = 'Video') | models.Q(model = 'Other') content_type = models.ForeignKey(ContentType, limit_choices_to = limit) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey('
content_type' , 'object_id') def __unicode__(self): return u"%s" % os.path.basename(self.content_object .url.name) def instance(self): return self.content_object.__class__.__name__ class Media(models.Model): description = models.CharField(blank = True, max_length = 500) link = models.URLField(blank = True) genericFK = generic.GenericRelation(GenericMedia , content_type_field='content_type' , object_id_field='object_id') class Meta: abstract = True def __unicode__(self): return u"%s" % os.path.basename(self.url.name) def save(self, *args, **kwargs): super(Media, self).save(*args, **kwargs) generic_link = GenericMedia(content_object = self) generic_link.save() class Image(Media): imgW = models.PositiveSmallIntegerField () imgH = models.PositiveSmallIntegerField () url = models.ImageField(upload_to = 'mediamanager', height_field = 'imgH', width_field = 'imgW')Everythings works fine, excepts the GenericRelation in my abstract Media Class.
In django documentation it is said that :
If you delete an object that has a GenericRelation, any objects which have a GenericForeignKey pointing at it will be deleted as well.But my problem is that when I delete an image (wich extends Media), the GenericMedia pointing to it is not deleted.
If anyone has a solution, thanks !
I don't understand why you are using generic relations at all here. Model subclassing and generic relations are *alternatives* to allow multiple model types to relate to a single destination. Since your subclasses already all inherit from Media, why do you need a generic relation between Media and GenericMedia? Why not a standard ForeignKey? Or why not inherit directly from GenericMedia in the first place?
--
DR.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7dddeb77-bc59-42ff-aca4-bfcaed29677f%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
No comments:
Post a Comment