Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6028033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:40:45+00:00 2026-05-23T04:40:45+00:00

I am trying to check and see if there is a duplicate entry in

  • 0

I am trying to check and see if there is a duplicate entry in the database before adding an entry. Here are my current models —

class Education(models.Model):
    school = models.CharField(max_length=100)
    class_year = models.IntegerField(max_length=4, blank=True, null=True, choices=YEAR)
    degree = models.CharField(max_length=100, blank=True, null=True)

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    employments = models.ManyToManyField(Employment)

On the form, a user must enter a school. Class year and degree are optional. To check for duplicate entries, right now I have —

if form.is_valid() and request.POST['school']:
    school = form.cleaned_data['school']
    try:
        school_object = Education.objects.get(school=form.cleaned_data['school'],
                                              class_year=form.cleaned_data['class_year'],
                                              degree = form.cleaned_data['degree'])
    except (Education.DoesNotExist):
        school_object = Education(school=form.cleaned_data['school'],
                                  class_year=form.cleaned_data['class_year'],
                                  degree = form.cleaned_data['degree'])
        school_object.save()
        profile.educations.add(school_object)
        profile.save()

I am getting an ValueError if the class_date is not filled out. How to fix this and also when checking for duplicates? Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:40:45+00:00Added an answer on May 23, 2026 at 4:40 am

    First, unless you’ve got a great reason you really shouldn’t be accessing post variables without sending them through a form.

    from django import forms
    class MyForm(forms.form):
      school = forms.CharField()
      degree = forms.CharField(required=False)
      class_year = forms.CharField(required=False)
    
      def clean(self):
        if not self.cleaned_data.has_key('degree'):
          self.cleaned_data['degree'] = None
        if not self.cleaned_data.has_key('class_year'):
          self.cleaned_data['class_year'] = None
        return self.cleaned_data
    

    Then when you’re processing the view:

    ...
    if request.method == "POST":
      form = MyForm(request.POST)
      if form.is_valid():
        try:
          # NOTE that the objects.get method will raise MultipleObjectsReturned if the
          # database has more than one object that matches the query
          my_object = Education.objects.get(school=form.cleaned_data['school'], 
            class_year=form.cleaned_data['class_year'], 
            degree = form.cleaned_data['degree'])
        except Education.DoesNotExist:
          my_object = Education(school=form.cleaned_data['school'], 
            class_year=form.cleaned_data['class_year'], 
            degree = form.cleaned_data['degree'])
    else:
      form = MyForm()
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello i am trying to check to see if there is a row in
I am trying to check a string to see if there are any misspelled
I have been trying to check my arraylist to see if an object already
I am trying to check a series of buttons to see if they have
I am trying to implement a check to see if the .NET framework is
I'm trying to check to see if a disk or disk image is 'empty'.
So yeah, I'm just trying to see if there is a more elegant way
I am trying to check to see if the user is looking at my
Here's a problem I've been trying to solve at work. I'm not a database
I am trying to check if certain ID already exists in database. When it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.