A
A
apheyhys2018-11-17 19:51:17
Django
apheyhys, 2018-11-17 19:51:17

How to access secondary model records?

There are two related tables.
Using the query
article = TestSplitModel.objects.get(id=article_id)
you can get the serial_number, title and description
how to get all Image records associated with an Article?

class Article(models.Model):
    serial_number = models.IntegerField()
    title = models.CharField(max_length=100)
    description = models.TextField()
    image = models.ManyToManyField('Image', blank=True)

class Image(models.Model):
    slug = models.SlugField(null=True)
    image = models.ImageField(upload_to=generate_filename)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-11-17
@apheyhys

The documentation contains many simple and understandable examples.

for image in article.image.all():
    ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question