A
A
Andrei1penguin12021-09-08 09:56:02
Django
Andrei1penguin1, 2021-09-08 09:56:02

How to get all ForeignKey links?

Good day, stuck on a simple task, there is a user model, he has a field with images:

class User(models.Model):
    ...
    images = models.ForeignKey("Image", on_delete=models.CASCADE, verbose_name="images")

class Image(models.Model):
    image = models.ImageField(verbose_name="image")

When creating a user, when accessing images (which initially do not exist and should not exist) by
User.objects.get(login=login).images
returning an error like relationdoesnotexist
Can you please tell me how to solve this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-09-08
@Andrei1penguin1

The task is to determine the number of photos and zero in case of their absence

User.objects.get(login=login).images.count()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question