Questions tagged [generic-relationship]

11 questions
7
votes
2 answers

Django Generic Relations and ORM Queries

Say I have the following models: class Image(models.Model): image = models.ImageField(max_length=200, upload_to=file_home) content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object =…
hanksims
  • 1,479
  • 2
  • 12
  • 22
6
votes
2 answers

get_or_create generic relations in Django & python debugging in general

I ran the code to create the generically related objects from this demo: http://www.djangoproject.com/documentation/models/generic_relations/ Everything is good intially: >>> bacon.tags.create(tag="fatty") >>> tag, newtag =…
rabidpebble
  • 567
  • 1
  • 5
  • 8
5
votes
3 answers

set contenttype by name in generic relation in django rest framework

class Foo(models.Model): bar = models.CharField(max_length=300) content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = GenericForeignKey('content_type', 'object_id') class…
matteok
  • 2,189
  • 3
  • 30
  • 54
5
votes
1 answer

How to filter/exclude inactive comments from my annotated Django query?

I'm using the object_list generic view to quickly list a set of Articles. Each Article has comments attached to it. The query uses an annotation to Count() the number of comments and then order_by() that annotated number. 'queryset':…
3
votes
1 answer

Django Generic Relations with Django Admin

I have a Django project, that has a "Address" model. This is used in several places - by a "User Profile" model, by a "Hospital" model, by an "Insitution" model etc. I'm using Django's generic relations to allow each of these objects to create a…
victorhooi
  • 16,775
  • 22
  • 90
  • 113
2
votes
1 answer

How do I traverse a generic relationship in a Django template?

I would like to traverse generic relationships in my Django template, similar to how you can traverse FK relationships. Models.py class Company(models.Model): name = models.CharField(blank=True, max_length=100) notes =…
Ben S
  • 1,407
  • 1
  • 13
  • 27
2
votes
1 answer

Django generic relations practice

i'm developing a authentication backend with object-based permissions for my django-app.I use generic relations between an object and a permission: class GroupPermission(models.Model): content_t=…
Dudevil
  • 23
  • 3
1
vote
0 answers

Ordering across a generic relationship in django

For the example: from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes import generic class Container(models.Model): pass class StoredUnit(models.Model): container = models.ForeignKey( Container ) …
dting
  • 38,604
  • 10
  • 95
  • 114
1
vote
0 answers

Represent a generic decision tree with SQL Alchemy

I want to create a generic decision tree using SQL Alchemy. That is, each node has zero or more children of any type, and the task is to evaluate some expression using the tree root, which will pass on the logic to the children using extending…
Mugen
  • 8,301
  • 10
  • 62
  • 140
0
votes
3 answers

Django GenericRelation doesn't save related object's id - is this a bug or am I doing it wrong?

I have a model with a generic relation (call it A), when creating an instance of this object I pass an instance of another model (call it B) as the initializer of the content_object field (via kwargs of the constructor). If I don't save B before…
pinkeen
  • 690
  • 3
  • 10
  • 21
0
votes
1 answer

How can I retrieve all items associated with a model using a generic relation?

I am making a small browser-based game while trying to learn Django. I have many models which can all have "Items" associated with them. Here is one of the models that can contain items: class Bank(models.Model): user = models.ForeignKey(User,…