Questions tagged [generic-relations]
35 questions
2
votes
2 answers
Why can't I save my model with a generic relation twice in Django?
I got a model TrackedItem with a generic relation linking to any model it is supposed to track.
If I do that:
t = TrackedItem(content_object=MyModel)
t.save()
t.save()
I get :
IntegrityError: (1062, "Duplicate entry '1' for key 'PRIMARY'")
Indeed,…

Bite code
- 578,959
- 113
- 301
- 329
2
votes
1 answer
Django: Adding property to User model after creating model based on abstract class
I have a normal model and an abstract model like so:
class TaggedSubject(models.Model):
user = models.ForeignKey(User, null=True, blank=True)
category = models.CharField(max_length=200)
foo = models.CharField(max_length=50)
bar =…

Jordan Reiter
- 20,467
- 11
- 95
- 161
1
vote
0 answers
AttributeError: 'GenericRelatedObjectManager' object has no attribute
I tried to use generic relation in django project. but it gives attribute error.
Generic relation is between UniqueSourcePresenter and UniqueDbSource
i have an instance of TextTable which has a foreignkey attribute for UniqueSourcePresenter i tried…

İlyas Sarı
- 11
- 3
1
vote
1 answer
Cascade delete of model with GenericForeignKey without using GenericRelation
I'm creating a reusable django app which includes a model with GenericForeignKey which I need to be cascade deleted.
This model may be attached to any other. I have no control over target model class as it is outside of the app. This means I can not…

Dan Zaikin
- 197
- 3
- 14
1
vote
1 answer
Django 1.10: Error when deleting a model with a GenericRelation
In my project I am mapping models inheriting from class A to models of type B using a GenericRelation via a third model, ABMapping.
models.py:
class A(models.Model):
b = GenericRelation(B)
class Meta:
abstract = True
class…

TAH
- 1,658
- 1
- 19
- 37
1
vote
1 answer
How can I serialize (with Django REST API) when class relates to the same generic relation multiple times?
I have a class that has multiple references to the same generic class for different information:
class Resort(models.Model):
id = models.PositiveIntegerField(_('HapNr.'), primary_key=True)
symbol = models.CharField(_('Kurzzeichen'),…

RichardX
- 51
- 3
1
vote
2 answers
Python get GenericRelation objects
I have the following code:
data/telephone.py:
class TelephoneNumber(models.Model):
MOBILE = 0
HOME = 1
TELEFON_CHOICES = (
(MOBILE, _("Mobile")),
(HOME, _("Landline")),
)
object_id =…

Martin Grohmann
- 437
- 2
- 17
1
vote
1 answer
ValidationError with Django REST API and generic_relations for generic foreign key writable access
I'm using Django REST API and generic_relations module with it to create a kind of variant to Django Comments which is not driven by templates but via REST API functions.
The Model class in named Annotation and is like:
class…

Anshul
- 746
- 9
- 22
1
vote
1 answer
Complex queries across Django reverse generic relations: possible?
I have a class Image with the following GenericRelation:
properties = models.GenericRelation(Property)
I'm trying to get all Images with certain properties, so I do this:
Image.objects.filter(properties__type = "foo", properties__user =…

Salvatore Iovene
- 2,064
- 1
- 17
- 31
1
vote
1 answer
Django 1.5 ModelForm like admin in view with images and foreign key
I have the following models:
class Quiver(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
is_default = models.BooleanField(default=False)
type = models.CharField(max_length=1, choices=QUIVER_TYPES)
category =…

Dachmt
- 2,079
- 4
- 29
- 45
0
votes
1 answer
Django: get ValueQuerySet with values from generic relation
I have two models linked by a generic relation:
from django.contrib.contenttypes import generic
from django.db import models
class Bar(models.Model):
content_type = models.ForeignKey(ContentType)
object_id =…

Don
- 16,928
- 12
- 63
- 101
0
votes
1 answer
Django DRF and generic relations: how to obtain content_object field from API response?
I have implemented Generic Relations in Django DRF following the official guide and it works well, apart from the fact that I cannot seem to obtain the field content_object from my API response.
Basically, I have a model called Document that can…

Giulia
- 765
- 2
- 8
- 33
0
votes
1 answer
Django Generic Relation woth parent model
I have created a model Comments. I want store reply in same table Comment.
class Comment(models.Model):
user = models.ForeignKey(User, blank=True, null=True, on_delete=models.CASCADE)
content_type = models.ForeignKey(ContentType,…

JRudransh
- 77
- 13
0
votes
1 answer
Django serialize child model from inheritance field
I'm using DRF and I have what appears to be a design issue with my models:
Background:
I'm doing a personal project for finance administration, so I have models for SavingsAccount, CreditCard, Incomes (this is really simplified but I think that is…

Germán Ruelas
- 125
- 2
- 8
0
votes
1 answer
How to mark post as liked with django Generic Relation
I would like to use GenericRelation in my app to implement "like" feature.
I have already these models:
class Activity(models.Model):
LIKE = 'L'
ACTIVITY_TYPES = (
(LIKE, 'Like'),
)
user =…

user9192656
- 549
- 3
- 16