M
M
Margarita Gavrilova2020-02-21 16:30:51
Django
Margarita Gavrilova, 2020-02-21 16:30:51

How to display data in django model foreing key?

There are 2 models: develop (developer) and dom (house). It is necessary to display on the house page a link to the developer's page and the name of the developer. My version doesn't work.

html:

{% for develop in dom.develops.all %}
  <a href="{{ develop.get_absolute_url }}">{{ develop.name }}</a>
{% endfor %}


models.py
class develop(models.Model):
    ''''Застройщик'''
    name = models.CharField("Имя", max_length=100)
    tel = models.CharField("телефон", max_length=20)
    description = models.TextField("Описание")
    image = models.ImageField("Изображение", upload_to="develop/")
    url = models.SlugField(max_length=160, unique=True)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('develop_detail', kwargs={"slug": self.slug})

    class Meta:
        verbose_name = "Застройщик"
        verbose_name_plural = "Застройщики"

class dom(models.Model):
    '''Карточка новостроки'''
    title = models.CharField("дом", max_length=150)
    description = models.TextField("Описание")
    url = models.SlugField(max_length=160, unique=True)
    image = models.ImageField("Изображение", upload_to="dom/")
    develops = models.ForeignKey(
        develop, verbose_name="застройщик", on_delete=models.SET_NULL, null=True, related_name="dom_develop")
    adres = models.CharField("Адрес", max_length=250)
    koordinata = models.CharField("координаты", max_length=250)
    kvartir = models.CharField("Квартиры", max_length=250)
    draft = models.BooleanField("Черновик", default=False)
    burgetm = models.PositiveIntegerField("Цена", default=0, help_text="за квадрат")
    year = models.PositiveSmallIntegerField("Дата выхода", default=2019)
    budget = models.PositiveIntegerField("цена", default=0)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("dom_detail", kwargs={"slug": self.url})

views.py
class newdomsView(ListView):
    """Список фильмов"""
    model = dom
    queryset = dom.objects.filter(draft=False)
    template_name = "newdoms/dom_list.html"


class NovostroykaDetailView(DetailView):
    '''описание фильма'''
    model = dom
    slug_field = "url"

class developView(DetailView):
    '''описание застройщика'''
    model = develop
    template_name = 'newdoms/develop.html'
    slug_field = "url"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2020-02-21
@gawmax

ForeignKey points to one relationship. With what fright do you have a cycle in your template? Just

<a href="{{ dom.develops.get_absolute_url }}">{{ dom.develops.name }}</a>

PS Transliteration in the names of models and fields is a signal of a low level of professionalism, avoid this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question