python – Checking for duplicates – Education Career Blog

I have a small problem. I am trying to check to see if status’s value already exists and make sure I do not create another instance of it, but I am having some trouble. Ex. If the project status was once “Quote” I do not want to be able make the status “Quote” again. Right now, I check to make sure if the user selects edit, then clicks submit, the status doesnt duplicate. However, if the User selected another status, like “completed” nothing stops them from going back in and selecting “quote” again.

class Status(models.Model):
   project = models.ForeignKey(Project, related_name='status')
   value = models.CharField(max_length=20, choices=STATUS_CHOICES, verbose_name='Status')
   date_created= models.DateTimeField(auto_now=True) 

class Project(models.Model):

 if form.is_valid():    
        project =
        project.created_by = request.user 
        old_status = project.current_status()
        if not old_status or old_status.value != form.cleaned_data.get('status', None):
             #add status instance
                  value = form.cleaned_data.get('status', None)

        return HttpResponseRedirect('/project/')

Any help, or pointing me in the right direction would be much appreciated.

Thanks everyone!


value = models.CharField(max_length=20, choices=STATUS_CHOICES, verbose_name='Status', unique=True)

Leave a Comment