A
A
Anton Tarara2016-02-18 07:41:37
Django
Anton Tarara, 2016-02-18 07:41:37

How to work with data in intermediate table in Django ORM?

How to work with data in an intermediate table using Django ORM, with a many-to-many relationship?
For example, table A and B are linked through table A_B and it has fields A_id B_id and let's say the Value field, how to work with it if we select entity a and want to display all related entities B plus this value?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
IvanOne, 2016-02-18
@atarara

Your answer is here https://docs.djangoproject.com/es/1.9/topics/db/ex...
as an option:

class Publication(models.Model):
    title = models.CharField(max_length=30)

    def __str__(self):              # __unicode__ on Python 2
        return self.title

    class Meta:
        ordering = ('title',)

class Article(models.Model):
    headline = models.CharField(max_length=100)
    publications = models.ManyToManyField(Publication)

    def __str__(self):              # __unicode__ on Python 2
        return self.headline

    class Meta:
        ordering = ('headline',)

Publication.objects.get(id=4).article_set.all()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question