C
C
Cyrus2014-12-08 10:50:42
Django
Cyrus, 2014-12-08 10:50:42

What is the best way to infer forms from a model?

Good afternoon, tell me this question. I want to bring this class into a form, but I can’t decide how it will be right, it seems like it would be better to use Modelformset or you can get by with ModelForm.

сlass Track(models.Model):

    person = models.ForeignKey(Person, verbose_name=u'Кто принял')
    city = models.ForeignKey(City, verbose_name=u'Город', null=True)
    street = models.ForeignKey(Street, verbose_name=u'Улица', null=True)
    building = models.ForeignKey(Building, verbose_name=u'№ дома', null=True)
    flat = models.ForeignKey(Flat, verbose_name=u'№ квартиры', null=True)
    ticket_type = models.ForeignKey(Ticket_type, null=True)
    status = models.ForeignKey(Status, null=True)
    port = models.SmallIntegerField(verbose_name=u'Порт абонента', default=0)
    note = models.TextField(verbose_name=u'Описание заявки', blank=True)
    TIME_WORK_BEGIN = (
        ('7-00', '7-00'),
        ('8-00', '8-00'),
        ('9-00', '9-00'),
        ('10-00', '10-00'),
        ('11-00', '11-00'),
        ('12-00', '12-00'),
        ('13-00', '13-00'),
        ('14-00', '14-00'),
        ('15-00', '15-00'),
        ('16-00', '16-00'),
        ('17-00', '17-00'),
        ('18-00', '18-00'),
        ('19-00', '19-00'),
        ('20-00', '20-00'),
        ('21-00', '21-00')
    )
    time_work = models.CharField(verbose_name=u'Время начала работ', max_length=5, choices=TIME_WORK_BEGIN)
    TIME_WORK_END = (
        ('7-00', '7-00'),
        ('8-00', '8-00'),
        ('9-00', '9-00'),
        ('10-00', '10-00'),
        ('11-00', '11-00'),
        ('12-00', '12-00'),
        ('13-00', '13-00'),
        ('14-00', '14-00'),
        ('15-00', '15-00'),
        ('16-00', '16-00'),
        ('17-00', '17-00'),
        ('18-00', '18-00'),
        ('19-00', '19-00'),
        ('20-00', '20-00'),
        ('21-00', '21-00')
    )
    time_work_end = models.CharField(verbose_name=u'Время окончания работ', max_length=5, choices=TIME_WORK_END)
    date_work = models.DateField(verbose_name=u'Дата работ', null=True)
    responsible_work = models.ManyToManyField(Person, related_name='+', verbose_name=u'Кому передана')
    create_date = models.DateTimeField(auto_now_add=True)
    last_mode_date = models.DateTimeField(auto_now=True)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Ali Aliyev, 2014-12-08
@Novakuz

One ModelForm is enough. Try crispy_forms:

class TrackForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(TrackForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper(self)
        self.helper.layout.append(Submit('save', 'save'))

    class Meta:
        model = Track

in templates {% crispy form %}

A
Andrey K, 2014-12-08
@mututunus

FormSet'y are necessary only for creation/editing of several objects at the same time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question