Questions tagged [django-generic-relations]
44 questions
1
vote
2 answers
Prefetching model with GenericForeignKey
I have a data structure in which a Document has many Blocks which have exactly one Paragraph or Header. A simplified implementation:
class Document(models.Model):
title = models.CharField()
class Block(models.Model):
document =…

nehalem
- 397
- 2
- 20
1
vote
1 answer
How to link 3rd party model and view with own model in Django
I successfully implemented django-contrib-comments to my project and starting to adjust it step by step to make it 100% suitable to my project. Now I have the following issue which I cannot solve:
I have a model Poller which offers two possible…

JSRB
- 2,492
- 1
- 17
- 48
1
vote
1 answer
How are 'comments' tied to 'article' in django-comments package?
In Django's own comment framework, django-contrib-comments, if I create my own comment model as below:
from django_comments.models import Comment
class MyCommentModel(Comment):
Q: How should I associate this new comment model (MyCommentModel) with…

Yan Tian
- 377
- 3
- 11
1
vote
1 answer
How to use foreignkey with different models in DJango
I'm stack with this problem, and looking for some solution before asking this question here. Lets say I have a one Model that shares same field in different Models how can I achieve this that when I get the objects of those Models I can include that…

User Unknown
- 1,079
- 1
- 7
- 17
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
0 answers
why does not django create db_index for object_id for GenericForeignkey?
i am using django 2.2. i have create a like/dislikie system using generic relation. django only create a indexing for contenttype but not object_id. if i try to count likes/dislike for specific model with specific object id it will be slow because…

ashley parker
- 45
- 8
1
vote
1 answer
django-import-export how to handle GenericRelations?
I'm using django-import-export module to export the record. However, I couldn't export the generic relations. I just want to get all the details of GenericRelation.
Found the snippet below in Github but it doesn't work.
class…

Wreeecks
- 2,186
- 1
- 33
- 53
1
vote
1 answer
Django refering to same model instances in Abstract Model
I have an abstract model from which a couple of my main models are inherited. Main difficulty in this case is that I have a need in reference to the same model, like a ForeignKey to self. I have read that the ForeignKey is not possible in abstract…

Michael
- 1,170
- 2
- 14
- 35
1
vote
0 answers
Django GenericRelation vs. JSONField
In my application a number of different kinds of Records can be mapped to a Person. In my initial implementation, I made each record inherit from an abstract base class and used GenericRelations to manage the whole thing. The code is a bit…

TAH
- 1,658
- 1
- 19
- 37
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
2 answers
DRF serializer with generic foreign key - check if given object ID exists before saving
I'm trying to find a way of checking whether given object ID in IntegerField exists (which is used for generic relation in serializer), like there is for PrimaryKeyRelatedField.
So far, I came with this approach:
models.py:
class…

softzer0
- 445
- 2
- 7
- 25
1
vote
1 answer
In Django, how to populate a generic relation from a single form field?
In our application, we have model A that has fields for a generic relation, because it could be related to an instance of either one of two models (B or C).
When populating a non-generic ForeignKey field, we can use the ModelChoiceField.
Our problem…

Ad N
- 7,930
- 6
- 36
- 80
0
votes
2 answers
Problem traversing generic relationship with different types of primary keys
I have the following models (greatly simplified)
class Species(models.Model):
# automatic primary key: id, bigint
label = models.CharField(
null=True, blank=True,
max_length=LABEL_LENGTH
)
photos =…

Angelika Sajani
- 43
- 5
0
votes
0 answers
How to apply custom validation on GenericTabularInline, TabularInline (related objects) in Django admin?
This is a question about GenericTabularInline, TabularInline, and admin.py.
models.py
class Chapters(models.Model):
name = models.CharField(max_length=40, unique=True)
class Projects(models.Model):
name =…

shraysalvi
- 303
- 1
- 10
0
votes
0 answers
How to Reduce Queries while Querying/Filtering GenericRelated Field Object
I have a Like model whose code looks like this:
class Like(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
liked_on = models.DateTimeField(auto_now_add=True)
content_type = models.ForeignKey(ContentType,…

Olusola Caleb
- 21
- 5