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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:21:27+00:00 2026-06-04T05:21:27+00:00

I have a Link and a Bookmark model like this: class Link(models.Model): url =

  • 0

I have a Link and a Bookmark model like this:

class Link(models.Model):
    url = models.URLField(unique=True)        
    def __unicode__(self):
        return self.url

class Bookmark(models.Model):
    title=models.CharField(max_length=200)
    user=models.ForeignKey(User)
    link=models.ForeignKey(Link)        
    def __unicode__(self):
        return u'%s, %s' % (self.user.username, self.link.url)

Now within a view I see if a Link with a given url already exists.
This object is then passed next with the username to Bookmarks collection to see if a bookmark already exists with this username and Link instance already exists.

def bookmark_save_page(request):
    if request.method == 'POST':
        form = BookmarkSaveForm(request.POST)
        if form.is_valid():
            # Create or get Link
            link, dummy = Link.objects.get_or_create(url=form.cleaned_data['url'])
            # Create or get bookmark
            bookmark, created = Bookmark.objects.get_or_create(user=request.user, link=link)

            # Save bookmark to database
            bookmark.save()
            return HttpResponseRedirect('/user/%s/' % request.user.username)

This is the bit I don’t understand. How does it know how to take the url field inside Link model as a way of comparison? Is it because I had defined it in the Link model like this?

def __unicode__(self):
        return self.url

I am coming from .NET and there you have to define the GetHash() for the class as a way to specify how the instances should be compared against each other.

How does Python know this?
Thanks

  • 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-04T05:21:29+00:00Added an answer on June 4, 2026 at 5:21 am

    I think you are asking “how does Django compare instances when filtering”, rather than “how does python compare objects”.

    With the following line of code,

    bookmark, created = Bookmark.objects.get_or_create(user=request.user, link=link)
    

    Django is filtering on link object’s primary key. The __unicode__ method does not matter.

    See the Django docs for comparing objects and queries over related objects for more info.

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

Sidebar

Related Questions

Let's say I have a structure like this: <folder name=folder1> <folder name=folder2> <bookmark href=link.html>
I have link that calls a function when clicked: <a href=javascript:spawnMenu(this); id=link1>Test1</a> To make
I have this link in my Struts2 app: <a href=/MyApp/My.action?w=%E8%A8%80%E8%91%89&key=6f98f58ce>Link</a> %E8%A8%80%E8%91%89 is shown as
I have a link that I dynamically create which looks something like the following:
I have a link that opens a Thickbox... <a href=page.php?tag=lists-element&keepThis=true&TB_iframe=true&height=450&width=500> Everything works with this
I have a link to some style sheet from the Yahoo Developer Network, this
I have a 'Continue Reading' function that looks like this: function twentyten_continue_reading_link() { global
I have been really stuck on this problem. My validation for my model fails
I have a url.LoginID, and I'd like to remove it from the address bar
I have a link tag which assignes a bookmark icon to the page: <link

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.