• Как сделать чтобы форма сохраняла данные в две модели?

    @bielikovv Автор вопроса
    Kadabrov, я имею модель, которая расширяет модель User:
    class Profile(models.Model):
        user = models.OneToOneField(User, on_delete=models.CASCADE, verbose_name="Username")
        photo = models.ImageField(verbose_name='Photo', upload_to='photo/%Y/%m/%d', blank=True)
        location = models.CharField(verbose_name="Location", max_length=30, blank=True)
        birth_date = models.DateField(null=True, blank=True, verbose_name="Date of birth")
        about = models.TextField(blank=True, verbose_name="About")
        nickname = models.CharField(max_length=128, verbose_name="Singer nickname", null=True)


    Во views.py вот такая функция:
    def show_user_form(request):
        if request.method == 'POST':
            #в данной строке ниже данные сохраняются через инстанс(сохраняются только в модель Profile)
            #нужно, чтобы другая часть данных сохранялась и в модель User
            form = RedactInfoUserForm(request.POST, request.FILES, instance=request.user.profile)
            if form.is_valid():
                form.save()
                redirect('profile')
        else:
            form = RedactInfoUserForm(instance=request.user.profile, initial={'first_name':request.user.first_name, 'last_name':request.user.last_name, 'email':request.user.email })
        return render(request, 'musiccloud/user_profile.html', {'form':form})


    Так же форма, которая редактирует данные в моделях User и Profile:
    class RedactInfoUserForm(forms.ModelForm):
        photo = forms.ImageField(label='Photo', widget=forms.FileInput(attrs={'class': 'form-control form-control-sm'}))
        first_name = forms.CharField(label='First name', widget=forms.TextInput(attrs={'class': 'form-control form-control-sm'}))
        last_name = forms.CharField(label='Last name', widget=forms.TextInput(attrs={'class': 'form-control form-control-sm'}))
        nickname = forms.CharField(label='Singer nickname', widget=forms.TextInput(attrs={'class': 'form-control form-control-sm'}))
        about = forms.CharField(label='About me', widget=forms.Textarea(attrs={'class': 'form-control form-control-sm'}))
        email = forms.EmailField(label='E-mail', widget=forms.EmailInput(attrs={'class': 'form-control form-control-sm'}))
        location = forms.CharField(label='My location', widget=forms.TextInput(attrs={'class': 'form-control form-control-sm'}))
        birth_date = forms.DateField(label='Date of birth', widget=forms.DateInput(attrs={'class': 'form-control form-control-sm'}))
    
        class Meta:
            model = User
            fields = ['photo', 'first_name', 'last_name', 'nickname', 'email', 'about', 'location', 'birth_date']