Ребят, бьюсь который день уже. Никак не получется наполнить связную модель в шаблоне. ЗАполняется модель User, но не заполняется модель Band.
models.py
from django.db import models
from django.contrib.auth.models import User
class Band(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, default="")
band_name = models.CharField(max_length=50)
phone = models.CharField(max_length=15)
vk_page = models.URLField(blank=True, null=True)
class Repetition(models.Model):
band = models.ForeignKey(Band, on_delete=models.CASCADE)
start_time = models.CharField(max_length=5)
end_time = models.CharField(max_length=5)
date = models.DateField()
def user_post_save(sender, instance,**kwargs):
(profile, new) = Band.objects.get_or_create(user=instance)
def save_user_profile(sender, instance, **kwargs):
instance.band.save()
models.signals.post_save.connect(user_post_save, sender=User)
models.signals.post_save.connect(save_user_profile, sender=User)
forms.py
from django import forms
from .models import User, Repetition, Band
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ('username', 'password')
class BandForm(forms.ModelForm):
class Meta:
model = Band
fields = ('band_name', 'phone', 'vk_page',)
password = forms.CharField(widget=forms.PasswordInput, label="Пароль")
class RepetitionForm(forms.ModelForm):
class Meta:
model = Repetition
fields = ('start_time', 'end_time', 'date')
views.py
from django.contrib import messages
from django.shortcuts import render, redirect
from .forms import BandForm, RepetitionForm
from .models import Band, Repetition, User
def band_form(request):
user = User()
if request.method == 'POST':
_band_form = BandForm(request.POST, instance=user)
_repetition_form = RepetitionForm(request.POST)
if _band_form.is_valid():
data = _band_form.cleaned_data
user.username = data['band_name']
user.set_password(data['password'])
_band_form.save()
return redirect('/')
else:
messages.error(request, 'Whoo!')
else:
_band_form = BandForm(instance=user)
_repetition_form = RepetitionForm()
return render(request, 'auth.html', {'band_form': _band_form, 'repetition_form': _repetition_form})