0

I have simple model in django that looks like :

class TimeTable(models.Model):
    title = models.CharField(max_length=100)
    start_time= models.CharField(choices=MY_CHOICES, max_length=10)
    end_time = models.CharField(choices=MY_CHOICES, max_length=10)
    day1 = models.BooleanField(default=False)
    day2 = models.BooleanField(default=False)
    day3 = models.BooleanField(default=False)
    day4 = models.BooleanField(default=False)
    day5 = models.BooleanField(default=False)
    day6 = models.BooleanField(default=False)
    day7 = models.BooleanField(default=False)

for this model I have 2 forms :

class TimeTableForm(ModelForm):
    class Meta:
        model = TimeTable
        fields = ['title ', 'start_time', 'end_time']


class WeekDayForm(ModelForm):
    class Meta:
        model = TimeTable
        fields = ['day1', 'day2', 'day3', 'day4', 'day5', 'day6', 'day7']

Now In views.py I need to save this values into database

def schedule(request):
    if request.method == 'POST':
        form = TimeTableForm(request.POST)
        day_week = WeekDayForm(request.POST)   
        if all([form.is_valid(), day_week.is_valid()]):
            form.save()
            day_week.save()

I'm new in Django so I was thinking that this values will be combined into one and I will get proper data but for every one submit I get two separated objects in database for example

title | 8:00 | 10:00 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |

      |      |       | 0 | 1 | 1 | 1 | 1 | 0 | 0 |

but I should get

title | 8:00 | 10:00 | 0 | 1 | 1 | 1 | 1 | 0 | 0 | 

As you see this two forms values are separated in database (probably for person who have more much common with django it is obvious) there is an option to combine this two into one ?

KyluAce
  • 933
  • 1
  • 8
  • 25
  • Why do you have two forms? – Daniel Roseman Nov 27 '18 at 13:50
  • @DanielRoseman maybe it is stupid but in html i have two different style for them I mean : `'{{ schedule_form.as_p }} {{ week_day.as_table }}'`. It was fast way to do that – KyluAce Nov 27 '18 at 14:02
  • 1
    It won't work like that, make one single form and style each field separately. Look here: https://docs.djangoproject.com/en/2.1/topics/forms/#rendering-fields-manually – Nikita Tonkoskur Nov 27 '18 at 14:06

0 Answers0