0

I'm having some troubles to create a form in Django, I don't know if my aproaches are correct soy any help of where I have to read will be appreciated.

I want to show a field compose by two fields, one for hours and the other for minutes, reading Django documentation I came out with the idea to use MultiValueField, and I tried this:

class CreateEventForm(forms.ModelForm):

    class TimeField(forms.MultiValueField):
        def __init__(self, **kwargs):
        
            error_messages = {
                'incomplete': 'Missing info',
            }

            fields = (
                forms.CharField(max_length=64,
                label= "Horas"),
                forms.CharField(max_length=64,
                label="Minutos")
            )
    
            super().__init__(
                error_messages=error_messages,
                require_all_fields=False,
                fields=fields 
            )

journal_start = TimeField()
journal_finish = TimeField()

class Meta:
    model = UserEvent
    fields = ['duration', 'pre_time', 'post_time']
    labels = {
        'duration': 'Duración',
        'pre_time': 'Previo',
        'post_time': 'Después: ',
    }

But instead of having two different fields for starting and finishing journey I only have one, this is the picture of the output enter image description here

I've see other questions related with MultiValueFields likes this: Django: MultiValueField and MultiWidget and these other example: https://gist.github.com/elena/3915748 But the first talks abou other subject and the last one seems to use some libraries that I don't want to use, at least for now.

How can I make a Django form field with two inputs using Model Forms?

thanks for reading me!

0 Answers0