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 8082297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:02:50+00:00 2026-06-05T17:02:50+00:00

I have a problem with django model objects where I have overridden __hash__ ()

  • 0

I have a problem with django model objects where I have overridden __hash__() for some slightly complex uniqueness/distinctiveness constraints that I want to enforce when I use them. This is working fine with objects that I have just directly instantiated in memory, but not with ones I retrieve from the database.

Like this:

class Animal(models.Model):
    name = model.Charfield('name', max_length=10)

    def__hash__(self):
        return len(self.name) # silly example for purposes of illustration

and then this:

>> a = models.Animal(name='cat')
>> b = models.Animal(name='dog')
>> len(set((a,b))
> 1
>> a.save()
>> b.save()
>> len(set(models.Animal.objects.all()))
> 2

Hmm. Whatever hash function is being used here, it ain’t mine. I guess it’s something related to lazy fetching / objects not yet in a fully instantiated state, but how to get round it?

  • 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-06-05T17:02:54+00:00Added an answer on June 5, 2026 at 5:02 pm

    This is because you have implemented __hash__ without implementing __eq__. Implement __eq__ as well and you should be good to go.

    The length of set([a, b]) is 1 because Django defines a default __eq__ function which compares the primary keys of the objects — so before they are saved, both with have an id of None, so a == b will be True. After they are saved, they will both have been assigned different primary keys, so a != b will be True.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an odd problem with Django. I have a set of objects that
I have a problem with calling a property() inside a dynamic model in Django.
I have a problem with the jquery-autocomplete pluging and my django script. I want
I have some sort of a design problem with my Django AJAX application. I
today a weird problem occurred to me: I have a model class in Django
I have multiple Django models that reference a model using foreign keys as such:
I have a strange database problem in django, using sqlite: In the Model PrivateMessage:
I have a problem in displaying specific columns in my model in django... I
I have a custom property on my Django model that returns the full name
I would like to have a model in Django that has multiple pictures associated

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.