P
P
Pavel Korchagin2020-08-11 10:16:37
Django
Pavel Korchagin, 2020-08-11 10:16:37

How to display fields from another model in a form via FK?

Hello. Please help. I can not figure out how to display fields in the form from another model.
Those. I want to display the fields "CITY", "GROUP", "FIO" from the Numbers model (and use them as a filter.), To add an entry to the "CLEAR" model, indicating in the "MEMBERS" field the login of the user who made the entry.

in models.py

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

сlass Cities(models.Model):

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

class Numbers(models.Model):
    
    FIO = models.CharField('ФИО', max_length=255)
    WINLOGIN = models.CharField('Winlogin', max_length=255, unique=True)
    GROUP = models.ForeignKey(Group, on_delete=models.PROTECT, verbose_name='Группа')
    CITY = models.ForeignKey(Cities, on_delete=models.PROTECT, verbose_name='Город')
    DATE_ADD = models.DateTimeField('Дата добавления', auto_now_add=True)
    DATE_DELETE = models.DateTimeField('Дата удаления', blank=True, null=True)
    DATE_UPDATE = models.DateTimeField('Дата обновления', auto_now=True, null=True)
    MEMBERS = models.CharField('Логин изменившего', max_length=255, null=True, blank=True)
    
    def __str__(self):
        return self.FIO

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')
    ]

    FIO = models.ForeignKey(Numbers, verbose_name='ФИО', 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)
    DATE_ADD = models.DateTimeField('Дата добавления', auto_now_add=True)
    DATE_DELETE = models.DateTimeField('Дата удаления', blank=True, null=True)
    DATE_UPDATE = models.DateTimeField('Дата обновления', auto_now=True, null=True)
    MEMBERS = models.CharField('Логин изменившего', max_length=255, null=True, blank=True)


In urls.py

path('add_clear/', views.AddClear.as_view(), name='add_clear'),


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


in Forms.py

class ClearForm(forms.ModelForm):
    class Meta:
        model = Clear
        fields = ['FIO', 'DATE_CLEAR', 'HOUR_FROM', 'HOUR_TO', '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'


and the html template itself:

{% 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 %}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2020-08-11
@tumbler

Read about choices in forms, but in general MEMBERS should be a ForeignKey on the User model.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question