F
F
forli2020-05-24 23:26:07
Django
forli, 2020-05-24 23:26:07

How to connect the model with the author?

There is a form with an order, i.e. it should be so that an authorized user makes an order, and his profile displays his orders, i.e. Product Name.
But the problem is, if you place an order through the admin panel and immediately put a buyer, then purchases are displayed in the profile, but not through the form itself, it gives an error

IntegrityError at /BoxShop/
(1048, "Column 'author_id' cannot be null")


those. the author (buyer) field cannot be empty, but it seems like I indicated in the view ((in general, here is the code

class Zayavka(models.Model):
    GEEKS_CHOICES = (
        ("Sweet БОКС", "Sweet БОКС"),
        ("СЮРПРИЗ БОКС", "СЮРПРИЗ БОКС"),
        ("Блогер бокс", "Блогер бокс"),
        ("8 Марта бокс", "8 Марта бокс"),
        ("Halloween бокс", "Halloween бокс"),
        ("14 февраля бокс", "14 февраля бокс"),
        ("Новогодний бокс", "Новогодний бокс"),
        # ("Пепси", "Пепси"),
        # ("Макси чай", "Макси чай"),
    )

    tovar = models.CharField('Товат', max_length=50, choices=GEEKS_CHOICES)
    kolvo = models.IntegerField('Количество')
    subject = models.CharField('Тема', max_length=100)
    sender = models.EmailField('Email', max_length=50)
    message = models.TextField('Сообщение')
    author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

    def __str__(self):
        return self.tovar

class ContactForm(forms.ModelForm):
    class Meta:
        model = Zayavka
        fields = ('tovar','kolvo','subject','sender','message')


def contactform(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            tovar = form.cleaned_data['tovar']
            kolvo = form.cleaned_data['kolvo']
            subject = form.cleaned_data['subject']
            sender = form.cleaned_data['sender']
            message = form.cleaned_data['message']

            recepients = ['[email protected]']

            try:
                send_mail(subject,
                          'Имя: ' + subject + '\n' 'Номер телефона: ' + message + '\n' + 'Email: ' + sender + '\n' + 'Товар: ' + tovar + '\n' + 'Количество: ' + str(
                              kolvo), '[email protected]', recepients)
            except BadHeaderError:
                return HttpResponse('Invalid header found')
            form.save()
            return redirect('thanks')
    else:
        form = ContactForm()
    return render(request,'Main.html',{'form':form})


Профиль:

def account_view(request):
    if not request.user.is_authenticated:
        return redirect('feedback')

    context = {}

    zayavki = Zayavka.objects.filter(<b>author=request.user</b>) # Вот здесь я определяю, что заявка должна быть прикреплена к юзеру.
    context['zayavki'] = zayavki

    return render(request,'account.html',context)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question