an abstraction used in Django to allow a foreign key relationship to be with any model.
Questions tagged [generic-foreign-key]
108 questions
54
votes
2 answers
django: how do I query based on GenericForeignKey's fields?
I'm new in using GenericForeignKey, and I couldn't make it to work in a query statement. The tables are roughly like the following:
class Ticket(models.Model):
issue_ct = models.ForeignKey(ContentType, related_name='issue_content_type')
…

Shang Wang
- 24,909
- 20
- 73
- 94
27
votes
3 answers
Why won't my GenericForeignKey cascade when deleting?
I'm creating a custom commenting system which can attache comments to any model using the contenttypes GenericForeignKey.
class Comment(models.Model):
body = models.TextField(verbose_name='Comment')
user = models.ForeignKey(User)
parent…

Soviut
- 88,194
- 49
- 192
- 260
27
votes
1 answer
How to traverse a GenericForeignKey in Django?
I'm using Django v1.9.4 with PostgreSQL 9.2.14 behind. With the following models:
from django.db import models
from django.contrib.contenttypes.fields import GenericRelation, GenericForeignKey
from django.contrib.contenttypes.models import…

wim
- 338,267
- 99
- 616
- 750
21
votes
2 answers
sqlalchemy generic foreign key (like in django ORM)
Does sqlalchemy have something like django's GenericForeignKey? And is it right to use generic foreign fields.
My problem is: I have several models (for example, Post, Project, Vacancy, nothing special there) and I want to add comments to each of…

krasulya
- 511
- 6
- 17
21
votes
3 answers
Django: Example of generic relations using the contenttypes framework?
I've pored over the Django docs regarding the contenttypes framework several times, and I simply don't understand it well enough to implement generic relations in my project. I've looked for online examples or tutorials regarding the matter, but I…

sgarza62
- 5,998
- 8
- 49
- 69
17
votes
1 answer
django: prefetch related objects of a GenericForeignKey
Suppose I have a model Box with a GenericForeignKey that points to either an Apple instance or a Chocolate instance. Apple and Chocolate, in turn, have ForeignKeys to Farm and Factory, respectively. I want to display a list of Boxes, for which I…

cberzan
- 2,009
- 1
- 21
- 32
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
12
votes
3 answers
Is it possible to use a natural key for a GenericForeignKey in Django?
I have the following:
target_content_type = models.ForeignKey(ContentType, related_name='target_content_type')
target_object_id = models.PositiveIntegerField()
target = generic.GenericForeignKey('target_content_type', 'target_object_id')
I would…

Riz
- 123
- 2
- 5
9
votes
2 answers
Django: Can I use objects.filter() for generic foreignkey?
symbol.py
class Symbol(BaseModel):
name = models.CharField(max_length=30,)
class Meta:
abstract = True
class StockSymbol(Symbol):
market = models.CharField(max_length=10,)
my_daily_price =…

user3595632
- 5,380
- 10
- 55
- 111
8
votes
1 answer
Generic Relations/Generic Foreign Keys in the Django Admin
I've been trying to display a GenericForeignKey in the Django admin but can't get it working. I have a FullCitation class that can be linked to either a NonSupportedProgram or a SupportedProgram class. So, I have used a generic foreign key.
In the…

steph
- 701
- 2
- 10
- 30
7
votes
4 answers
In Django/South HOWTO create an instance of a model from a different app during DataMigration
I need to perform a datamigration of a model Answer in app Question. In that script there is a dependency such that I need to create an instance of a model Chapter which is in the app Journal. So, I coded it as follows:
def forwards(self, orm):
…

Chantz
- 5,883
- 10
- 56
- 79
7
votes
3 answers
GenericForeignKey data migtation error: 'content_object' is an invalid keyword argument
I want to create data migrations for a model(Comment) which has a GenericForeignKey relation. My model was made according to django documentation for contenttypes.
Models:
...
class NiceMeme(models.Model):
"""
Example model.
"""
…

Laraconda
- 667
- 6
- 15
7
votes
2 answers
Django Generic Foreign Key Filtering (difference between v1.5 & v1.6)
I have the following conceptual models:
class GenericAbstractBase(models.Model):
name = models.CharField(max_length=255)
staff = generic.GenericRelation(
"Staff",
content_type_field="content_type",
…

randlet
- 3,628
- 1
- 17
- 21
7
votes
1 answer
GenericForeignKey, ContentType and DjangoRestFramework
I am working on an discussion app in Django, that has Threads, Posts, Replies and Votes. Votes uses Generic Foreign Keys and Content Types to ensure a user can only vote once on a specific Thread/Post/Reply.
Vote model looks like this:
VOTE_TYPE =…

chrisg
- 115
- 1
- 4