I have a page that has a form where people can put a name, and create a profile. The problem is when several people try to put the same name, since it gives this error get() returned more than one Person
What I'm trying to do is to avoid having the same name created twice in the database.
This is the view, in which I tried to search the database for the name that has been set, and if it finds it, it stops the process of adding the name to the database, but for some reason it does not work:
def index(request):
if request.method == "POST":
form = PersonaForm(request.POST)
elnombre=form.save(commit=False).nombre
busqueda = ('a'+str(Persona.objects.filter(nombre=elnombre)))
queryset = ('a'+'<QuerySet [<Persona: '+str(elnombre)+'>]>')
if busqueda == sendmessage:
#Aqui se haria algo para detener la creacion del perfil
else:
if form.is_valid():
persona = form.save(commit=False)
persona.save()
return redirect('persona', nombre=persona.nombre)
else:
form = PersonaForm()
return render(request, 'crush/index.html', {'form': form})
So, how could I avoid creating the same object several times in the database?
The Person model:
class Persona(models.Model):
nombre = models.CharField(max_length=30, blank=False,)
def __str__(self):
return self.nombre
def __unicode__(self):
return self.nombre