Answer the question
In order to leave comments, you need to log in
Django ForeignKey how to filter output?
Hello everyone, here is such a model:
class Booking(models.Model):
full_name = models.CharField(
u'ФИО',
max_length=128,
default=u'[читатель]',
)
author = models.CharField(
verbose_name=u'автор',
max_length=128,
)
title = models.CharField(
verbose_name=u'заглавие книги/журнала',
max_length=128,
)
library = models.ForeignKey(
Library,
verbose_name=u'библиотека',
)
comment = models.TextField(
verbose_name=u'комментарий',
blank=True,
null=True,
)
created_at = models.DateTimeField(
verbose_name=u'запрос отправлен',
auto_now_add=True,
)
updated_at = models.DateTimeField(
verbose_name=u'последнее изменение',
auto_now=True,
)
is_booking = models.CharField(
verbose_name=u'бронирование',
max_length=7,
choices=(
('undec', u'Нет решения'),
('allow', u'Забронировано'),
('dec', u'Бронирование невозможно')
),
default='undec',
)
decision_email = models.BooleanField(
verbose_name=u'письмо отправлено',
default=False,
editable=False
)
email = models.EmailField(
u'электонная почта',
max_length=128
)
class Library(models.Model):
title = models.CharField(
max_length=120,
verbose_name=u'название'
)
slug = models.SlugField(
verbose_name=u'URL'
)
lead = models.CharField(blank=True, null=True, max_length=120, editable=False, verbose_name=u'Краткое описание')
description = models.TextField(verbose_name=u'Полное описание', default=u'', null=True, editable=False)
photo = FileBrowseField(directory='library_photo', max_length=255, blank=True, null=True, editable=False, verbose_name=u'Фотография')
address = models.CharField(
verbose_name=u'Адрес',
max_length=220,
default=u'',
blank=True,
null=True,
)
Answer the question
In order to leave comments, you need to log in
The essence of the question is not entirely clear. If you know the criterion by which some part is not displayed (or displayed), use in the form:
class YourBookingForm(ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) # super(YourBookingForm, self) для python2
self.fields['library'].queryset = Library.objects.exclude(pk=4) # Удаляем из выборки по условию ненужные библиотеки
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question