Reputation: 25
Python django how I can prevent the duplicate entry of studnumber, email, username (unmae) in registration? I use this code before when it us one full name its work but when I divide it to (nmane),(mname),(nnmae), it always saying duplicate record. even their is no similar record on db
MODELS.py
:
from django import forms
from django.db import models
class newacc(models.Model):
studnumber=models.IntegerField()
fname=models.CharField(max_length=150)
mname=models.CharField(max_length=150)
lname=models.CharField(max_length=150)
age=models.IntegerField()
gender=models.CharField(max_length=1)
uname=models.CharField(max_length=150)
email=models.CharField(max_length=150)
pwd=models.CharField(max_length=150)
contact=models.IntegerField()
class Meta:
unique_together = ('studnumber','email','uname')
class NewACCForm(forms.ModelForm):
class Meta:
model = newacc
fields = "__all__"
Views.py
from django.shortcuts import redirect, render
from register.models import newacc
from django.contrib import messages
from django.db.models import Q#disjunction sa email and uname:
from register.models import NewACCForm
def Unreg(request):
if request.method=='POST':
form = NewACCForm(request.POST)
if form.is_valid():
form.save()
messages.success(request,"The new user is saved!")
else:
messages.error(request, "Duplicate record.")
return render(request,'Registration.html')
Upvotes: 0
Views: 548
Reputation: 96
Why not to make them unique individually !!
Add unique=True
Upvotes: 1