V
V
Vampre2018-05-05 15:56:10
Django
Vampre, 2018-05-05 15:56:10

How to display custom results in django?

There are three models:

# Модель мероприятия
class Event(models.Model):
    title = models.CharField("Название мероприятия", max_length=25)
    location = models.CharField("Место проведения", max_length=25)

# Модель простого поста в блоге
class Post(models.Model):
    title = models.CharField("Заголовок поста", max_length=50)
    body = models.TextField("Текст поста", blank=True, null=True)
    pub_date = models.DateTimeField("Дата публикации", default=timezone.now)

# Модель поста привязанного к конкретному мероприятию
class EventPost(Post):
    event = models.ForeignKey(Event, on_delete=models.CASCADE)

The bottom line is that you need to display all posts on the blog (related to events and simple ones) by the date they were added, but a plate with the name of the event and place is added to posts from events in the template. Those. I can combine two queries and pass them to the template, but I can’t figure out how to pull values ​​from the event model into the die.
It occurred to me to make a property in the EventPost model something like
@property
def location(self):
    location = Event.objects.get(pk=self.event)

But it seems to me that this is not a very correct option (and it’s not a fact that it’s working))

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question