Django GenericRelation not working

134 views
Skip to first unread message

arthur...@gmail.com

unread,
Jan 2, 2014, 4:09:22 PM1/2/14
to django...@googlegroups.com
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 !

Daniel Roseman

unread,
Jan 3, 2014, 4:58:39 AM1/3/14
to django...@googlegroups.com
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. 

arthur...@gmail.com

unread,
Jan 3, 2014, 8:36:48 AM1/3/14
to django...@googlegroups.com
Thanks Daniel for your answer.

I didn't know I could use a simple FK in this case. I will try using it. I didn't want to directly inherit from GenericMedia because I don't want to merge ContentType datas (content_type, content_object, etc.) with my own Media datas (description, link, ...)

So far I just see a problem I'm going to meet in my save method :

I will change my Media model to this one : 

class Media(models.Model):
    description     = models.CharField(blank = True, max_length = 500)
    link            = models.URLField(blank = True)
    generic_fk      = models.OneToOneField(GenericMedia)

    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()
        generic_fk = generic_link.id

But I think I will have a problem with the save method, Media have a FK to GenericMedia but GenericMedia have to Media, so how could I adapt ? Thanks !
Reply all
Reply to author
Forward
0 new messages