0
I’m developing a quotation site for language study purposes. I have customized the User models and it includes other attributes such as address and city for example. The problem now is to write the logged in user ID to the related field, that is, my quotation table has a field client
who receives the id
of the logged-in user, so that when creating the quotation this id
be saved. If I do this by Admin it works! If I add the 'client' field via forms.py
by the Meta class, it works! because I can choose the logged in user or another one from the list. But the right thing is to record the id directly without having to show or choose user.
What I did wrong in the codes below?
models.py
from django.db import models
from qnow_user.models import User
from django.conf import settings
from datetime import datetime,date
from django.contrib.auth import get_user_model
# Create your models here.
class Quotation(models.Model):
#User owner quotation
client = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE, related_name='quotation',
help_text='Cliente dono desta cotação')
#Date create of quotation
date_create = models.DateField('Data Criação',default=date.today,
help_text='Data em que foi criada esta cotação.')
date_update = models.DateField('Data Atualização',default=date.today,
help_text='Data em que foi modificada esta cotação.')
#House type to quotation
HOUSETYPECHOICES = (
('Apartamento', 'Apartamento'),
('Casa', 'Casa'),
('Comercial', 'Comercial'),
('Escritório', 'Escritório'),
('Outros', 'Outros'),
)
house_type = models.CharField('Tipo do Imóvel',max_length=20,
choices=HOUSETYPECHOICES,blank=False,default='Casa',
help_text='Especifique o seu tipo de imóvel')
STATUSCHOICES = (
(0,'Pendente'), #Client requested quotation
(1,'Em Análise'), #Company analizing quotation
(2,'Liberado'), #Quotation released for provider
(3,'Orçado'), #Quotation provider
)
status = models.IntegerField('Situação do Pedido',choices=STATUSCHOICES, blank=True,default=0,
help_text='Difinia a situação atual desta cotação.')
#House set to quotation
house_set = models.CharField('Tipo de Residencial', max_length=100, default='Casa particular',
help_text='Especifique o nome do seu condomínio ou residencial.')
class Meta:
verbose_name = 'Cotação'
verbose_name_plural = 'Cotações'
def __str__(self):
return self.house_set
forms.py
from django import forms
from .models import Quotation
from django.contrib.auth import get_user_model
from qnow_user.models import User
User = get_user_model()
class QuotationForm(forms.ModelForm):
class Meta:
model = Quotation
fields = ('client','date_create', 'date_update', 'house_type',
'house_set',
)
views.py
from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponse, HttpResponseNotFound
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth import authenticate, login
from django.http import HttpResponseRedirect
import datetime
from .models import Quotation
from .forms import QuotationForm
@login_required
def quotation_client(request):
template_name = '../templates/client_quotation.html'
form = QuotationForm(request.POST)
if request.method == 'POST':
#if request.user.is_authenticated:
# quotation_client = Quotation.objects.get_or_create(client=request.user)
if form.is_valid():
form.save(commit=True)
return HttpResponse("<h1>Cotação Feita</h1>")
else:
form = QuotationForm()
else:
form = QuotationForm()
print('tetse')
context = {
'active_page_client_provider' : 'active',
'form':form
}
return render(request,template_name,context)
#return HttpResponse("<h1>Área do cliente no cliente2</h1>")
admin.py
from django.contrib import admin
from .models import Quotation
# Register your models here.
admin.site.register(Quotation)