Django GenericRelation not working

2014-01-02 Thread arthur . muchir
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 !

-- 
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+unsubscr...@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/9a090654-0298-4cfd-a71b-c815cca53ee0%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Django GenericRelation not working

2014-01-03 Thread arthur . muchir
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 !

Le jeudi 2 janvier 2014 22:09:22 UTC+1, arthur...@gmail.com a écrit :
>
> 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 !
>
>

-- 
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+unsubscr...@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/3b5f0c65-bd17-410c-8813-5ef721b321d6%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.