Unique_together and OneToOne field constraints error catching in django form -


i have model:

class person(models.model):     name = models.charfield(max_length=250)     slug = autoslugfield(populate_from='name')     birth_date = models.datefield(null=true, blank=true)     blood_group = models.charfield(max_length=5)     present_address = models.charfield(max_length=250, blank=true)     permanent_address = models.charfield(max_length=250, blank=true)     user = models.onetoonefield(         settings.auth_user_model,         related_name='member_persons')      class meta:         ordering = ['name']         unique_together = ['name', 'birth_date'] 

you see above model has 2 constraints: 1 unique user , unique_together name , birth_date fields. want when submit create form simultaneously check both unique user same name , birth date people can't submit form. when happens should show appropriate errors in form fields. of other thread of so far write clean method follows:

def clean(self):     user = get_user(self.request)     name = self.cleaned_data.get('name')     birth_date = self.cleaned_data.get('birth_date')     if person.objects.filter(user=user).exists():         self.add_error('name', "you submitted data")     elif person.objects.filter(name=name, birth_date=birth_date).exists():         self.add_error('name', "this name , birth date exists")     return self.cleaned_data 

it worked fine , responding both of field constraints. question method efficient? if not suggest better method that. using class-based view.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -