Questions tagged [django-generic-relations]
44 questions
15
votes
1 answer
Error Using CheckConstraint in Model.Meta along with Django GenericForeignKey - Joined field references are not permitted in this query
I am trying to restrict GFK to be pointed to objects of a few models only, and I thought CheckConstraint will be a great way to do this, however I get this error
class ManualAdjustment(Model):
content_type = models.ForeignKey(ContentType,…

dowjones123
- 3,695
- 5
- 40
- 83
13
votes
2 answers
How to use inverse of a GenericRelation
I must be really misunderstanding something with the GenericRelation field from Django's content types framework.
To create a minimal self contained example, I will use the polls example app from the tutorial. Add a generic foreign key field into…

wim
- 338,267
- 99
- 616
- 750
6
votes
2 answers
GenericForeignKey gets wrong id when used with model with UUIDField
When using GenericForeignKey together with UUIDField, what is the recommended way to get a queryset of the "real model" from a queryset of generic objects?
Here are the models I'm testing with:
import uuid
from django.contrib.contenttypes.fields…

Oskar Persson
- 6,605
- 15
- 63
- 124
5
votes
1 answer
Dealing GenericRelation and GenricForeignKey inside migrations
I have models with GenricForeigKey and GenericRelation fields.
class Datasheet(models.Model):
package1 = GenericRelation('PackageInstance')
...
class PackageInstance(models.Model):
content_object = GenericForeignKey()
object_id =…

David R.
- 855
- 8
- 17
5
votes
2 answers
Django Admin Generic content type multiple models inline form
I'm getting started with Django and I'm a bit stuck on a multi-models field, AKA Generic Relation (Content Type)
I have a generic content type "student_solution" that can belong to either:
a Org model
a Institution model
a Campus model
Therefore,…

Vadorequest
- 16,593
- 24
- 118
- 215
4
votes
2 answers
Django Image model with dynamic upload_to function using generic relation
I am creating an Image model which will be used by other models via generic relation. For example, Newsposts and events will have images. Below is the sample Image model
class Image(models.Model):
description = models.CharField(max_length=500,…

grustamli
- 396
- 4
- 16
3
votes
1 answer
How to migrate old data to django-quill-editor
I had a textField in my model before. Now I want to upgrade it to a rich text field using Django Quill Editor. But I am unable the migrate the old data.
I tried to just copy the text inside the quillfield in a management command but it gives…

yukashima huksay
- 5,834
- 7
- 45
- 78
3
votes
1 answer
How to add unique_together with generic foreign key in Django
I am not sure if I am doing it wrong or there is some issue when handling unique constraint when working with GenericForeign Relations in Django.
When I try to save an object (in Admin for example) I get unique constraint error (raises 500) form…

Adeel
- 761
- 7
- 24
2
votes
2 answers
Django filter on generic relationship (unique constraint exception)
I have a model below which points to a generic relationship. This can either be a Post object or a Reply object.
class ReportedContent(models.Model):
reporter = models.ForeignKey(User, on_delete=models.CASCADE)
# Generic relation for posts…

Aymane Max
- 182
- 11
2
votes
0 answers
Django ORM - Filter by GenericRelation across multiple models
Filtering on Django GenericRelations has been implemented 4 years ago via https://code.djangoproject.com/ticket/22207 and supports now to filter from the related model:
class Issue(models.Model):
project_content_type =…

Mario
- 2,619
- 1
- 24
- 22
2
votes
2 answers
Generic foreign key in Django showing error must be instance of Content Type
I have a following abstract Class
class Manufacturer(models.Model):
company=models.CharField(max_length=255)
class Meta:
abstract = True
Now 2 classes inherit from above:-
class Car(Manufacturer):
name =…

Harkirat Saluja
- 7,768
- 5
- 47
- 73
2
votes
1 answer
Serialize Generic relationships with Django Rest Framework, with write support
I have the following model:
class TaggedItem(models.Model):
tag = models.SlugField()
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object =…

roob
- 2,419
- 3
- 29
- 45
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
0 answers
Use content_object to add an object instead of object_id, in generic relations
I have a model(profile) which has generic relation with another model(member) in another app. When I want to add a new profile object I have to use object_id which is a field that shows the ID of that member object which has relation with this…

Farbod Khalili
- 51
- 4
1
vote
1 answer
How to prefetch or use select related in reverse generic relation in django?
Assume i have two models:
class A:
content_type = models.ForeignKey(ContentType, on_delete=models.PROTECT, null=True, blank=True)
object_id = models.PositiveIntegerField(null=True, blank=True)
attached_object =…

aashish manandhar
- 49
- 6