P
P
Pavel Korchagin2020-08-07 15:43:09
Django
Pavel Korchagin, 2020-08-07 15:43:09

How to make Createview with filters for a field from another model?

I am using Django 3.0.8 and Python 3.8. There are models:
models.py

class group(models.Model):
    group = models.CharField('Группа', max_length=5, unique=True)
    
def __str__(self):
        return self.group

class city(models.Model):
    tspk = models.CharField('Город', max_length=255, unique=True)
    
def __str__(self):
        return self.tspk

class chisl(models.Model):
    FIO = models.CharField('ФИО', max_length=255)
    WINLOGIN = models.CharField('Winlogin', max_length=255, unique=True)
    GROUP_ID = models.ForeignKey(group, on_delete=models.PROTECT, verbose_name='Группа')
    TSPK_ID = models.ForeignKey(city, on_delete=models.PROTECT, verbose_name='Город')
    def __str__(self):
        return self.WINLOGIN

class clear(models.Model):
    chas = [
        ('0', '0'), ('1', '1'), ('2', '2'), ('3', '3'),
        ('4', '4'), ('5', '5'), ('6', '6'),
        ('7', '7'), ('8', '8'), ('9', '9'),
        ('10', '10'), ('11', '11'), ('12', '12'),
        ('13', '13'), ('14', '14'), ('15', '15'),
        ('16', '16'), ('17', '17'), ('18', '18'),
        ('19', '19'), ('20', '20'), ('21', '21'),
        ('22', '22'), ('23', '23')
    ]

    WINLOGIN = models.ForeignKey(chisl, related_name='Winlogin', verbose_name='winlogin', on_delete=models.PROTECT)
    DATE_CLEAR = models.DateField('Дата чистки')
    HOUR_FROM = models.CharField('с', max_length=2, choices=chas)
    HOUR_TO = models.CharField('по', max_length=2, choices=chas)
    COMMENT = models.CharField('Комментарий', max_length=255, null=True, blank=True)
    CAUSE = models.CharField('Причина', max_length=255, null=True, blank=True)
      
    def __unicode__(self):
        return u"%i" % self.WINLOGIN


At the moment there is forms.py
class ClearForm(forms.ModelForm):
    class Meta:
        model = clear
        fields = ['WINLOGIN', 'DATE_CLEAR', 'HOUR_FROM', 'HOUR_TO', 'CAUSE',  'COMMENT']

    def clean(self):
        hour_from = self.cleaned_data['HOUR_FROM']
        hour_to = self.cleaned_data['HOUR_TO']
        if hour_from > hour_to:
            raise ValidationError('"Время C" должно быть меньше')
        return self.cleaned_data

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for field in self.fields:
            self.fields[field].widget.attrs['class'] = 'form-control'


in views.py file
class Clear(PermissionRequiredMixin, CreateView):
    permission_required = 'clear.add_clear'
    model = clear
    template_name = 'clear/add_clear.html'
    form_class = ClearForm
    success_url = reverse_lazy('line2clear')
    extra_context = {'title': 'Добавить в чистку'}


in urls.py file
path('add_line2clear/', views.AddLine2Clear.as_view(), name='add_clear'),


in html file:

{% extends 'base.html' %}

{% block title %}{{ title }}{% endblock %}

{% block content %}
<h1 class="mt-3">{{ title }}</h1>
<div class="col-3 ml-4">
<form class="mt-4" action="{% url 'add_clear' %}" method = 'post'>
    {% csrf_token %}
{{ form }}

        <button type="submit" class="btn btn-danger btn-block mt-2">Добавить запись</button>


</form>
</div>
{% endblock %}


I want to make a city, group filter in the form of adding an entry, and display the name of the city, group satisfying the filters in the drop-down list.

But no matter what I try to do, I get errors. I would really appreciate any help or information on how to solve this problem.

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