J
J
justdani2017-12-06 13:39:40
Kali
justdani, 2017-12-06 13:39:40

I can't populate a linked table in django. What's wrong?

Guys, I've been fighting for a day already. There is no way to fill the connected model in the template. The User model is populated, but the Band model is not populated.
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})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rostislav Grigoriev, 2017-12-06
@justdani

Because:

  1. _band_form = BandForm(request.POST, instance=user)- here the instance of the User model is set, but the Band is needed.
  2. If the Band instance does not already exist, it does not need to be transferred
  3. In the meta of the BandForm form for fields, the user field is not specified, which means it will not be saved even if such a parameter is passed.
  4. user = User()- Unsaved user instance is useless as ForeignKey for Band . You need to save it first

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question