Z
Z
zagogulka2022-04-04 17:15:40
Django
zagogulka, 2022-04-04 17:15:40

How to edit fields in a database table record using forms?

there is a table - Memo, which indicates the required material, its quantity and units of measurement.

Code under the spoiler
models.py

class SluzSnab(models.Model):
    sls_date = models.DateField('Дата создания служебной записки', blank=True, null=True)
    sls_no = models.CharField(max_length=20, verbose_name='Номер служебной записки', unique=True)
    sls_zak = models.CharField(max_length=20, verbose_name='Номер заказа')
    sls_kol = models.IntegerField(verbose_name='Количество изделий')
    sls_con_choice = [
        ('constr1', 'Иванов'),
        ('constr2', 'Петров'),
        ('constr3', 'Сидоров'),
    ]
    sls_opis = models.TextField(max_length=2000, verbose_name='Краткое описание', blank=True, null=True)
    sls_con = models.CharField(max_length=20, choices=sls_con_choice, verbose_name='Ответственный конструктор')
    sls_stat = [
        ('vyp', 'Выполнено'),
    ]
    sls_ei_choice = [
        ('sht', 'Штуки'),
        ('kg', 'Килограммы'),
        ('mt', 'Метры'),
        ('kvm', 'Кв. метры'),
        ('kt', 'Комплекты'),
        ('lt', 'Литры'),
        ('mlt', 'Миллилитры'),
    ]
    sls_meneg_choice = [
        ('manager1', 'Иванов'),
        ('manager2', 'Петров'),
        ('manager1', 'Сидоров'),
    ]
    sls_sogl_choice = [
        ('sogl', 'Согласовано'),
    ]
    sls_sogl = models.CharField(max_length=20, choices=sls_sogl_choice, verbose_name='Согласование начальником КО', blank=True, null=True)
    sls_name1 = models.CharField(max_length=500, verbose_name='Позиция1', blank=True, null=True)
    sls_ed1 = models.CharField(max_length=20, choices=sls_ei_choice, verbose_name='Единицы измерения', blank=True, null=True)
    sls_kol1 = models.IntegerField(verbose_name='Количество', blank=True, null=True)
    sls_vsego1 = models.IntegerField(blank=True, null=True, verbose_name='Всего (на заказ)')
    sls_manage1 = models.CharField(max_length=200, choices=sls_meneg_choice, verbose_name='Ответственный менеджер', blank=True, null=True)
    sls_date1 = models.DateField('Дата поставки', blank=True, null=True)
    sls_vyp1 = models.CharField(max_length=20, choices=sls_stat, verbose_name='Отметка о выполнении', blank=True, null=True)
    sls_comment1 = models.CharField(max_length=500, verbose_name='Комментарий к позиции', blank=True, null=True)
    sls_name2 = models.CharField(max_length=500, verbose_name='Позиция2', blank=True, null=True)
    sls_ed2 = models.CharField(max_length=20, choices=sls_ei_choice, verbose_name='Единицы измерения', blank=True, null=True)
    sls_kol2 = models.IntegerField(verbose_name='Количество', blank=True, null=True)
    sls_vsego2 = models.IntegerField(blank=True, null=True, verbose_name='Всего (на заказ)')
    sls_manage2 = models.CharField(max_length=200, choices=sls_meneg_choice, verbose_name='Ответственный менеджер', blank=True, null=True)
    sls_date2 = models.DateField('Дата поставки', blank=True, null=True)
    sls_vyp2 = models.CharField(max_length=20, choices=sls_stat, verbose_name='Отметка о выполнении', blank=True, null=True)
    sls_comment2 = models.CharField(max_length=500, verbose_name='Комментарий к позиции', blank=True, null=True)
    sls_name3 = models.CharField(max_length=500, verbose_name='Позиция1', blank=True, null=True)
    sls_ed3 = models.CharField(max_length=20, choices=sls_ei_choice, verbose_name='Единицы измерения', blank=True, null=True)
    sls_kol3 = models.IntegerField(verbose_name='Количество', blank=True, null=True)
    sls_vsego3 = models.IntegerField(blank=True, null=True, verbose_name='Всего (на заказ)')
    sls_manage3 = models.CharField(max_length=200, choices=sls_meneg_choice, verbose_name='Ответственный менеджер', blank=True, null=True)
    sls_date3 = models.DateField('Дата поставки', blank=True, null=True)
    sls_vyp3 = models.CharField(max_length=20, choices=sls_stat, verbose_name='Отметка о выполнении', blank=True, null=True)
    sls_comment3 = models.CharField(max_length=500, verbose_name='Комментарий к позиции', blank=True, null=True)


views.py

@login_required(redirect_field_name='login')
def sluzwno(request, pk):
    sluz = get_object_or_404(SluzSnab, pk=pk)
    if request.method == 'POST':
        sform = SluzebkaSnabForm(request.POST, instance=sluz)
        if sform.is_valid():
            sluz = sform.save(commit=False)
            sluz.save()
            return redirect('sluzebki')
    else:
        sform = SluzebkaSnabForm(instance=sluz)
    return render(request, 'sluzwno.html', {'sluz': sluz, 'sform': sform})


forms.py

class SluzebkaSnabForm(forms.ModelForm):
    class Meta:
        model = SluzSnab
        fields = ['sls_manage1', 'sls_date1', 'sls_vyp1', 'sls_manage2', 'sls_date2', 'sls_vyp2', 'sls_manage3', 'sls_date3', 'sls_vyp3']
        widgets = {
            'sls_manage1': forms.Select(),
            'sls_date1': forms.DateInput(format='%d.%m.%y'),
            'sls_vyp1': forms.Select(),
            'sls_manage2': forms.Select(),
            'sls_date2': forms.DateInput(format='%d.%m.%y'),
            'sls_vyp2': forms.Select(),
            'sls_manage3': forms.Select(),
            'sls_date3': forms.DateInput(format='%d.%m.%y'),
            'sls_vyp3': forms.Select(),
}


The head of the Purchasing department assigns which of the managers to entrust the purchase of material. However, when one of the managers marks the execution of his part of the service, the remaining marks disappear (become empty, not filled). Most likely, this is due to the .save () method, but I still do not understand how to defeat this. Perhaps someone has come across this.... and can help

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