Questions tagged [generic-relations]
35 questions
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
11
votes
1 answer
How to write a django-rest-framework serializer / field to merge data from generic relations?
I have objects with a generic relation pointing to various other objects, and I need them to be merged (inlined) so the serialized objects look like one whole objects.
E.G:
class Enrollement(models.Model):
hq = models.ForeignKey(Hq)
…

Bite code
- 578,959
- 113
- 301
- 329
7
votes
1 answer
Django GenericRelation in model Mixin
I have mixin and model:
class Mixin(object):
field = GenericRelation('ModelWithGR')
class MyModel(Mixin, models.Model):
...
But django do not turn GenericRelation field into GenericRelatedObjectManager:
>>> m = MyModel()
>>>…

zymud
- 2,221
- 16
- 24
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
Django: Are multiple Generic Relations in one Model bad design?
I have a model with multiple Generic Relations that has become very complicated to use in my templates. The model is a 'Gig' or musical event that takes place at a 'Venue' and/or a 'Festival' and has a 'Musician' and/or an 'Ensemble'.
Where it gets…

Nahanaeli Schelling
- 1,235
- 1
- 12
- 17
4
votes
1 answer
django generic one-to-one relations, with cascading deletes
I'm trying to emulate an inheritance model using django's generic relations. So far, this is what I've come up with:
class Base(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
…

DMac the Destroyer
- 5,240
- 6
- 36
- 56
4
votes
1 answer
Any clue on this error with generic relation using Django Orm?
This is a fun one :-)
Working on an EAV, we inject a generic relationship handler at runtime in a model.
model_cls is any class, and a EavValue class have a generic relation pointing to it. It works fine from EavValues to a model_cls, but on the…

Bite code
- 578,959
- 113
- 301
- 329
4
votes
3 answers
How do I use GenericRelation with Django Rest Framework?
I want to include a model with a GenericRelation backrefrence in DRF
The docs indicate this should be easy ( just above: http://www.django-rest-framework.org/api-guide/relations/#manytomanyfields-with-a-through-model ) - but I am missing…

Chozabu
- 1,015
- 1
- 10
- 33
4
votes
1 answer
How to filter generic foreign keys?
I have these models:
class EventEntry(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
start_date = …

Mike Stoddart
- 488
- 7
- 21
3
votes
2 answers
Django GenericRelated field conditional Query raising 'GenericRelation' object has no attribute 'field'
I have an event object, there are other objects besides Notes that have a generic relation to Event, and which don't have an active field. Now I want to be able to write a query which excludes all Events where the active field of the Notes is False.…

rak1n
- 671
- 1
- 8
- 17
3
votes
1 answer
Django BaseGenericInlineFormSet forms not inheriting FormSet instance as form instance related_object
I'm using Django 1.8 and I have an Image class that looks like this:
# The `child` class
class Image(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
related_object =…

Ariel
- 3,383
- 4
- 43
- 58
3
votes
1 answer
Django Deleting a GenericForeignKey
I am trying to implement an activity feed following this tutorial.
I would like to delete an acitivity(ie a comment has been added)when the corresponding object, ie the comment itself, has been removed. This doesn't seem to cascade.
Is there any way…
user1056978
2
votes
1 answer
Django indirect generic relation
I've been trying to implement a tagging system for my application, where only certain tags are allowed for each content type.
I've tried setting the content type on the Tag model, and using this value on the TagAttribution model, and have got...…

Wilerson
- 347
- 1
- 5
- 14
2
votes
1 answer
Annotate in Django with generic relations
I am using django-hitcount to tally hits to my database objects. I want to count the hits by object to determine which object has the most hits in a given time range. The app has two models of interest here:
class Hit(models.Model):
created …

Ed.
- 4,439
- 10
- 60
- 78
2
votes
2 answers
Django model attribute to refer to arbitrary model instance
I'm working on a logging app in Django to record when models in other apps are created, changed, or deleted. All I really need to record is the user who did it, a timestamp, a type of action, and the item that was changed. The user, timestamp, and…

exupero
- 9,136
- 8
- 47
- 63