Пытаюсь постепенно изучать python с djnago. Столкнулся с трудностями при выводе в шаблон из связной модели.
urls
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.RealtyListView.as_view(), name ='realty'),
url(r'^(?P<pk>\d+)/$', views.RealtyDeailView.as_view(), name= 'realty-detail'),
]
models
class realty(models.Model):
ROOMS_CHOICES=(
('1', '1 ком'),
('2', '2 ком'),
('3', '3 ком'),
('4', '4 ком'),
('st', 'Студия'),
('ob', 'Общежитие')
)
RA_CHOICES =(
('jd', 'Железнодорожный'),
('kr', 'Кировский'),
('ln', 'Ленинский'),
('ok', 'Октябрьский'),
('sv', 'Свердловский'),
('sov', 'Советский'),
('cent', 'Центральный')
)
SAN_CHOICES =(
('sov', 'Совместный'),
('raz', 'Разделённый'),
)
adress = models.CharField(verbose_name = "Адресс объекта", blank = False, null = True, default = None, max_length = 150)
etaj = models.DecimalField(verbose_name = "Этаж", max_digits= 2, decimal_places = 0, blank = False, null = True, default = None)
ploshad = models.DecimalField(verbose_name = "Площадь", max_digits= 5, decimal_places = 2, blank = False, null = True, default = None)
ploshad_k = models.DecimalField(verbose_name = "Площадь кухни", max_digits= 5, decimal_places = 2, blank = False, null = True, default = None)
ploshad_j = models.DecimalField(verbose_name = "Жилая площадь", max_digits= 5, decimal_places = 2, blank = False, null = True, default = None)
ploshad_b = models.DecimalField(verbose_name = "Полоадь балкона", max_digits= 5, decimal_places = 2, blank = False, null = True, default = None)
san_usel = models.CharField(verbose_name = "Сан узел", blank = False, null = True, choices = SAN_CHOICES, max_length = 100)
price = models.DecimalField(verbose_name = "Цена", max_digits= 10, decimal_places = 2, blank = False, null = True, default = None)
price2 = models.DecimalField(verbose_name = "Цена за квадрат", max_digits= 8, decimal_places = 2, blank = True, null = True, default = None)
potolok = models.DecimalField(verbose_name = "Высота потолков", max_digits= 4, decimal_places = 2, blank = False, null = True, default = None)
rooms = models.CharField(verbose_name = "Количество комнат", blank = False, null = True, default = "1", choices= ROOMS_CHOICES, max_length = 15)
rayon = models.CharField(verbose_name = "Район", blank = False, null = True, choices = RA_CHOICES, max_length = 100)
planirovka = models.ForeignKey('planirovka', verbose_name = "Планировка", on_delete = models.CASCADE, default= None)
realty_type = models.ForeignKey('realty_type', verbose_name = "Тип недживижимости", on_delete = models.CASCADE, default= None)
realty_status = models.ForeignKey('realty_status', verbose_name = "Статус дома", on_delete = models.CASCADE, default= None)
otdelka = models.ForeignKey('otdelka', verbose_name = "Отделка", on_delete = models.CASCADE, default= None)
description = models.TextField(verbose_name = "Описание")
def __str__(self):
return "%s" % self.adress
#Тестим save и пост save
def save(self, *args, **kwargs):
self.price2 = self.price /self.ploshad
self.price2 = round(self.price2, 2)
super(realty, self).save(*args, **kwargs)
class Meta:
verbose_name = 'Квартира'
verbose_name_plural = 'Квартиры'
def get_absolute_url(self):
return "/realty/%i/" % self.id
class photos(models.Model):
image_admin = models.ForeignKey('realty', verbose_name = "Фотографии", on_delete = models.CASCADE, default= None)
image = models.ImageField(upload_to='imgage_objects')
class Meta:
verbose_name = "Фотография"
verbose_name_plural = "Фотографии"
class planirovka(models.Model):
title = models.CharField(verbose_name = "Название", blank = False, null = True, default = None, max_length = 150)
description = models.TextField(verbose_name = "Описание", blank = False, null = True, default = None)
def __str__(self):
return "%s" % self.title
class Meta:
verbose_name = "Планировка"
verbose_name_plural = "Планировки"
class otdelka(models.Model):
title = models.CharField(verbose_name = "Название", blank = False, null = True, default = None, max_length = 150)
description = models.TextField(verbose_name = "Описание", blank = False, null = True, default = None)
def __str__(self):
return "%s" % self.title
class Meta:
verbose_name = "Отделка"
verbose_name_plural = "Отделки"
class realty_type(models.Model):
title = models.CharField(verbose_name = "Название", blank = False, null = True, default = None, max_length = 150)
description = models.TextField(verbose_name = "Описание", blank = False, null = True, default = None)
def __str__(self):
return "%s" % self.title
class Meta:
verbose_name = "Тип недвижимости"
verbose_name_plural = "Типы недживижимости"
class realty_status(models.Model):
title = models.CharField(verbose_name = "Название", blank = False, null = True, default = None, max_length = 150)
description = models.TextField(verbose_name = "Описание", blank = False, null = True, default = None)
def __str__(self):
return "%s" % self.title
class Meta:
verbose_name = "Статус дома"
verbose_name_plural = "Статусы домов"
views
from django.shortcuts import render
from django.views import generic
from realty.models import *
# Create your views here.
##return render(request, 'realty/realty.html', locals())
class RealtyListView(generic.ListView):
model = realty
print('Скрипт отработал')
def photo(self):
photo = photos.objects.filter(id = 1)
class RealtyDeailView(generic.DetailView):
model = realty
Шаблоны
{{realty.id}}
{{type_realty.realty.title}}
{% extends 'base.html' %}
{% block content %}
<h1>Выберите свой новый дом!</h1>
{% if realty_list %}
<ul>
{% for realty in realty_list %}
<a href="{{realty.get_absolute_url}}">{{realty.adress}}</a>
<img src="" alt="">
{{p}}
</ul>
{% endfor %}
{% endif %}
{% endblock %}
Не могу понять как подцепить данные из свзяных моделей, для вывода данных из модели photos, сделал вот такой запрос
image = photos.objects.all()
однако не могу понять как вывести этот запрос в шаблон