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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:53:50+00:00 2026-06-12T21:53:50+00:00

I have a model which has a nullable foreign key relation with itself (this

  • 0

I have a model which has a nullable foreign key relation with itself (this ‘self’ can be any model which has this foreign key). This foreign key is a custom class which prevents cyclic relationships.

The call to super() in the __init__ contains null=True and blank=True and therefor need to be included in the inspection rules of South. This makes at least the schemamigration work, but the migrate still fails.

Following code shows: the custom foreign key, the inspection rules and the model using the foreign key.

# Foreign key
class ParentField(models.ForeignKey)
    def __init__(self, verbose_name=None, **kwargs):
        super(ParentField, self).__init__('self', verbose_name=verbose_name, null=True, blank=True, **kwargs)

    @staticmethod
    def checkcyclic(object, attr):
        '''Check for a cyclic relationship'''
        pass

# Introspection rules
add_introspection_rules([
    (
        [ParentField],
        [],
        {
            'null': ['null', {'default': True}],
            'blank': ['blank', {'default': True}],
        }
    )
], ["^test\.models\.fields\.ParentField"])

# Model
class Relation(Model):
    parent = ParentField(related_name='child_set')

Migrating gives the following error:

$ ./manage.py migrate
[..]
super(ParentField, self).__init__('self', verbose_name=verbose_name, null=True, blank=True, **kwargs)
TypeError: __init__() got multiple values for keyword argument 'to'

I’ve tried addding a rule like below, but that changed nothing.

'to': ['rel.to', {'default': 'test.models.Relation'}],

Can anyone tell me what I’m doing the wrong way or any hints to a solution for this?

  • 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-12T21:53:51+00:00Added an answer on June 12, 2026 at 9:53 pm

    In the end this solution worked for me, credits to Shai over at googlegroups:

    South, when it tries to build an instance of your custom field, collects (using
    the rules) values for the different kwargs of your class. One of these is the
    "to" argument; you see it specified in the migration code.

    Then, it takes the values it collected and passes them to a constructor. So,
    effectively, you get the call

    fields.ParentField(related_name='set', to=orm['module.ModelClass'])
    

    However, inside your __init__ this gets translated to

    models.ForeignKey('self', related_name='set', to=orm['module.ModelClass'])
    

    So there are two values for the 'to' argument — the first is the positional
    'self'.

    One solution, at least, is to do the same thing I recommended for the null and
    blank arguments — add it into kwargs, rather than directly to the call.

    kwargs.setdefault('to', 'self') 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Django model which has a foreign key that is optional. I
I have a model which has 2 fields, a and b like this: class
I have a Model which has some constants defined, like below: class Order(models.Model): WAITING
I have a model which has a certain behaviour implemented. class X { ....
I have a model which has a template field. This template is HTML and
I have a model which has many of another model but this model only
I have a model which has a method called 'has_voted'. It looks like this...
I have a model which has a collection of users. I'm looping through this
I have db.Model which has several properties as described below: class Doc(db.Model): docTitle =
I have this model which has Image field to be uploaded. It has a

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.