Questions tagged [django-related-manager]

django-related-manager refers to the RelatedManager class that is used in Django to manage one-to-many or many-to-many related context

django-related-manager refers to the RelatedManager class that is used in Django to manage one-to-many or many-to-many related context.

129 questions
17
votes
1 answer

Django ManyToMany relation to 'self' without backward relations

There are several question about backwards relations here, but I'm either too dumb to understand them or think they don't fit my case. I have model class MyModel(models.Model) stuff = models.ManyToManyField('self', related_name = 'combined+') I…
Odif Yltsaeb
  • 5,575
  • 12
  • 49
  • 80
15
votes
2 answers

django rest framework - backward serialization to avoid prefetch_related

I have two models, Item and ItemGroup: class ItemGroup(models.Model): group_name = models.CharField(max_length=50) # fields.. class Item(models.Model): item_name = models.CharField(max_length=50) item_group =…
15
votes
2 answers

Django Abstract Models setting related_name with underscores

I have an Abstract base model and 2 inheriting models, and I need to force the related_name to be in a specific format. class Animal(models.Model): legs = models.IntegerField(related_name='%(class)s') habitat = models.ForeignKey(Habitats,…
rtindru
  • 5,107
  • 9
  • 41
  • 59
15
votes
2 answers

Django: How to get data connected by ForeignKey via Template?

Since a few weeks I am learning Python and Django. Up to this point it has been enough to read the questions and the answers of other users.But now the moment of my first own question has come. I will try to describe my problem as best i can. My…
user1459531
  • 153
  • 1
  • 1
  • 4
9
votes
1 answer

In Django, how can you get all related objects with a particular User foreign Key

I have something like this: class Video(models.Model): user = models.ForeignKey(User, related_name='owner') ... and I'm trying to access all the videos a particular user has by doing something like: u =…
9-bits
  • 10,395
  • 21
  • 61
  • 83
8
votes
2 answers

Object needs to have a value for field "id" before this many-to-many relationship can be used in Django

I have the following code in my models.py: class Tag(models.Model): name = models.CharField(max_length=75) class Article(models.Model): tags = models.ManyToManyField(Tag) def save(self, *args, **kwargs): for tag in self.tags: …
darkhorse
  • 8,192
  • 21
  • 72
  • 148
7
votes
2 answers

Displaying uploaded images in the template - Django

I am trying to display uploaded images to a template for my imaginary vegetable catalogue. I have a page to add a new vegetable and upload images. The main template is a list of vegetables which will later have thumbnails. Now, when viewing the…
6
votes
2 answers

Why does Django's RelatedManager not cache the object the lookup was called from, on the destination object?

If I have the following models: class Fubar(models.Model): name = models.CharField() class Related(models.Model): fubar = models.ForeignKey(Fubar) I would expect that the ORM would magically cache the parent Fubar object if I accessed…
Darb
  • 1,463
  • 11
  • 10
6
votes
1 answer

How to use 'contains' with manytomany field?

I have a model: class Tasks(models.Model): name = models.CharField(max_length = 50, null = True, blank = True) assigned_to = models.ManyToManyField(User, null = True, blank = True) I have to execute a query tasks_for_myuser =…
5
votes
2 answers

Django rest framework serializer with reverse relation

I have two models where employee have relation with person model but person have no relation with employee model. Like: class Person(models.Model): name = models.CharField(max_length=100) address = models.CharField(max_length=100) class…
5
votes
1 answer

How to set OneToOneField to null?

I have model with a OneToOneField that may be null: solution_for = models.OneToOneField('QNAQuestion', related_name='solution', blank=True, null=True) Sometimes it has a value that needs to be removed in a view. What is the syntax to null it? It's…
4
votes
1 answer

complexity of a reverse django foreign key lookup

Assume I have a model like this: class Post(models.Model): name = models.CharField(max_length=25, unique=True) class Picture(models.Model): post = models.ForeignKey(to=Post, ondelete=models.CASCADE) image =…
fuxen_p
  • 55
  • 3
4
votes
1 answer

Django: Get all objects from a specific user

I have a problem when trying to display all the Announce objects from a user. My problem is : Consider that I am logged in as a user with an id=1. When I go to /users/1/ it displays all my posts. But the problem is when I want to display all the…
4
votes
2 answers

How can I add multiple instances to a Django reverse foreign key set in a minimal number of hits to the database?

For example, I have two models: class Person(models.Model): name = models.CharField(max_length=100) class Job(models.Model): title = models.CharField(max_length=100) person = models.ForeignKey(Person) I have a list of job ids-- job_ids…
4
votes
1 answer

Using Django RelatedField for custom join queries?

I'm curious whether a RelatedField can be used to cause custom join queries. I would like to apply this in django-parler, a multilingual app for Django. For example, when "slug" is a translated field, I would like to have: …
vdboor
  • 21,914
  • 12
  • 83
  • 96
1
2 3
8 9