I
I
IvanOne2015-01-20 14:47:52
MySQL
IvanOne, 2015-01-20 14:47:52

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
    )

And the Library model:
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,
    )

Actually, what is the task: in the template in the form, you need to Display a list of library objects, provided that one library does not participate in the selection according to a certain condition, this condition is not taken into account in the model. The question is actually how best to solve the problem:
1. In ForeignKey through the limit_choices_to argument, but then I don’t understand how to make a selection
2. In the model, add a property that can be used when setting a condition, but is it worth it? After all, the selection needs to be done only once on the project for everything
. Can you tell me more options?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2015-04-12
@deliro

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 question

Ask a Question

731 491 924 answers to any question