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

  • Home
  • SEARCH
  • 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 1808866
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:19:19+00:00 2026-05-17T06:19:19+00:00

I came in touch with a little problem while building a model with a

  • 0

I came in touch with a little problem while building a model with a foreign key to itself.

Here an example:

class Example (model.Model):
    parent = models.ForeignKey('self', null=True, blank=True)
    # and some other fields

After creating a new entry in the admin panel and going into this example for editing some content, I realize that I could set the parent to the current entry. But thats not what I wanted to get with the ForeignKey and the relation to itself.

Is it possible to disallow the link to itself?

Maybe it’s better to use a integer field with the right choices but I’m not quiet sure how to realize this an a smooth and Python like way.

  • 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-17T06:19:20+00:00Added an answer on May 17, 2026 at 6:19 am

    one way to do this would be to override the model’s clean method

    class Example(model.Model):
        #...
        def clean(self):
            if self.parent.id == self.id:
                raise ValidationError("no self referential models")
    

    this will be called as the second step of object validation and will prevent the object from being inserted in the database.

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

Sidebar

Related Questions

Came across a problem whereby I wanted the last time data was imported to
So, I've been making iOS apps since the first iPod touch came out, but
I never really came in touch with perl before, so I really hope, this
Came across the term pigeon computing while reading about how google works.. Can someone
I recently came in touch with the Oracle NoSQL Database . It appears as
So, while working on something that was having locking issues, a question came to
I just came across this article about WPF performance on Touch/Tablet devices. In the
I came across the following paragraph while reading bruce eckel..where he was trying to
Here's the deal, I had this app working since iPhone OS 3.0 came out,
I came across this problem,I have got an algorithm that I need to implement

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.