python - Multiple models validation in the same View - Django 1.11 -
i'm looking strategy validate uniqueness of keys of 4 models have relationships each other before being able save them in database.
in other words, check uniqueness of keys there no inconsistency in database when using model.save()
method, , use when unique keys unique.
following view below example, fields cnpj, address, , number respectively in legalperson
, email
, phone
, must unique.
class storeregistrationview(view): ''' classe responsavel pelo cadastro de lojas ''' def post(self, request): if request.method == 'post': #import pdb; pdb.set_trace() form = storeform(request.post) if form.is_valid(): lp = legalperson( cnpj = form.cleaned_data['cnpj'], corporate_name = form.cleaned_data['corporate_name'], fantasy_name = form.cleaned_data['fantasy_name'], state_inscription = form.cleaned_data['state_inscription'], municipal_inscription = form.cleaned_data['municipal_inscription'], ) lp.save() address = address( street = form.cleaned_data['street'], neighborhood = form.cleaned_data['neighborhood'], number = form.cleaned_data['number'], complement = form.cleaned_data['complement'], city = form.cleaned_data['city'], estate = form.cleaned_data['estate'], country = 'brasil', cep = form.cleaned_data['cep'], latitude = form.cleaned_data['latitude'], longitude = form.cleaned_data['longitude'], person = lp, ) address.save() email = email( address = form.cleaned_data['email'], person=lp, ) email.save() phone = phone( number=form.cleaned_data['phone_number'], person=lp, ) phone.save() # mensagem de sucesso que será disponibilizada para o usuário messages.success(request, 'cadastro de loja efetuado com sucesso.') return redirect('importar-produtos') messages.warning(request, 'erro durante o cadastro.') context = { 'store_form': storeform(), } return render(request, 'n3oadmin/store-registration.html', context)
i've been researching, , found models in django have validating methods model.full_clean()
, model.clean()
, , model.validate_unique()
.
instead of using regular form, should create multiple model forms.
class legalpersonform(forms.modelform): class meta: model = legalperson fields = [...] class = addressform(forms.modelform): class meta: model = legalperson exclude = ['person']
then, use model forms in view , template. model forms take care of validating unique constraints.
if request.method == 'post': address_form = addressform(request.post) legal_person_form = legalpersonform(request.post) if address_form.is_valid() , legal_person_form.is_valid(): person = legal_person_form.save() address = address_form.save(commit=false) address.person = person address.save() ... return redirect('importar-produtos') else: address_form = addressform() legal_person_form = legalpersonform() context = { 'address_form': address_form, 'legal_person_form': legal_person_form, } return render(request, 'n3oadmin/store-registration.html', context)
note can exclude foreign key fields in model form. then, in view, can save commit=false
, set foreign key, save instance.
note have instantiated blank forms addressform()
, legalpersonform
in else
block (for requests). means if form valid, see form errors in template instead of blank form.
Comments
Post a Comment