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

The Archive Base Latest Questions

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

I have a Django project that has multiple django apps. One of them has

  • 0

I have a Django project that has multiple django “apps”. One of them has models to represent data coming from an external source (I do not control this data).

I want my other apps to be able to have references to this “external app” but I want to avoid all the fuzz of the database integrity checks. I don’t want the db to have any constraints on these “soft foreign keys”.

Do you know how I can code a custom field that will emulate a real Django ForeignKey without creating a hard constraint on the database?

Maybe this already exists, but I didn’t have any luck on Google.

Thanks in advance for the help 🙂

NB: I’m aware of the generic relations system with the content_types. But I don’t want generic relations. I want specific relations to identified models only without hard integrity constraints.

EDIT:

I found related links:

  • Django ForeignKey which does not require referential integrity?
  • Understanding / mySQL aka tricking ForeignKey relationships in Django

But I didn’t find a proper answer to my question. 🙁

EDIT 2012, june 4:

I’ve looked deep into django’s code to find what needs to be done, but I think that simply subclassing ForeignKey will not be enough. Could you give me some directions on how to do this?

NB: I use South for managing my database schema, so I figure I’ll need to do something about that too. But it may be out of the subject here 🙂

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

    Yo guys,

    I managed to make what I wanted.

    First, I created a new field:

    from django.db.models.deletion import DO_NOTHING
    from django.db.models.fields.related import ForeignKey, ManyToOneRel
    
    class SoftForeignKey(ForeignKey):
        """
        This field behaves like a normal django ForeignKey only without hard database constraints.
        """
        def __init__(self, to, to_field=None, rel_class=ManyToOneRel, **kwargs):
            ForeignKey.__init__(self, to, to_field=to_field, rel_class=rel_class, **kwargs)
            self.on_delete = DO_NOTHING
    
        no_db_constraints = True
    

    Since I use South to manage my database schema, I had to add this:

    from south.modelsinspector import add_introspection_rules
    add_introspection_rules([], [r'^ecm\.lib\.softfk\.SoftForeignKey'])
    

    Then, I had to monkey patch south so that it takes the no_db_constraints parameter into account. There were two functions involved in the creation of FK constraints:

    from django.db.models.deletion import DO_NOTHING
    from django.db.models.fields.related import ForeignKey, ManyToOneRel
    from django.core.management.color import no_style
    from south.db.generic import DatabaseOperations, invalidate_table_constraints, flatten
    
    def column_sql(self, table_name, field_name, field, tablespace='', with_name=True, field_prepared=False):
        """
        Creates the SQL snippet for a column. Used by add_column and add_table.
        """
    
        # If the field hasn't already been told its attribute name, do so.
    ...
    ...
    ...
    
            if field.rel and self.supports_foreign_keys:
                # HACK: "soft" FK handling begin
                if not hasattr(field, 'no_db_constraints') or not field.no_db_constraints:
                    self.add_deferred_sql(
                        self.foreign_key_sql(
                            table_name,
                            field.column,
                            field.rel.to._meta.db_table,
                            field.rel.to._meta.get_field(field.rel.field_name).column
                        )
                    )
                # HACK: "soft" FK handling end
    
        # Things like the contrib.gis module fields have this in 1.1 and below
        if hasattr(field, 'post_create_sql'):
            for stmt in field.post_create_sql(no_style(), ta
    ....
    ....
    
    # monkey patch South here
    DatabaseOperations.column_sql = column_sql
    

    And:

    from django.db.models.deletion import DO_NOTHING
    from django.db.models.fields.related import ForeignKey, ManyToOneRel
    from django.core.management.color import no_style
    from south.db.generic import DatabaseOperations, invalidate_table_constraints, flatten
    
    @invalidate_table_constraints
    def alter_column(self, table_name, name, field, explicit_name=True, ignore_constraints=False):
        """
        Alters the given column name so it will match the given field.
        Note that conversion between the two by the database must be possible.
        Will not automatically add _id by default; to have this behavour, pass
        explicit_name=False.
    
        @param table_name: The name of the table to add the column to
        @param name: The name of the column to alter
        @param field: The new field definition to use
        """
    
        if self.dry_run:
            if self.debug:
    ...
    ...
        if not ignore_constraints:
            # Add back FK constraints if needed
            if field.rel and self.supports_foreign_keys:
                # HACK: "soft" FK handling begin
                if not hasattr(field, 'no_db_constraints') or not field.no_db_constraints:
                    self.execute(
                        self.foreign_key_sql(
                            table_name,
                            field.column,
                            field.rel.to._meta.db_table,
                            field.rel.to._meta.get_field(field.rel.field_name).column
                        )
                    )
                # HACK: "soft" FK handling end
    
    # monkey patch South here
    DatabaseOperations.alter_column = alter_column
    

    This is really ugly but I didn’t find another way.

    Now you can use the SoftForeignKey field exactly like a normal ForeignKey except that you won’t have any referencial integrity enforcement.

    See here for the complete monkey-patch : http://eve-corp-management.org/projects/ecm/repository/entry/ecm/lib/softfk.py

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

Sidebar

Related Questions

I have a Django project that, on one page, has multiple forms (in different
I have a Django project that has two models: Group and Person. Groups can
I have a Django project which has two apps (one created as debug test).
I have an existing Django project that has several models using concrete inheritance of
I have a django project which has two apps, one is AppA and AppB
I have Django project on Dreamhost server which has several views that returns Json
i have set up buildout project (django to be specific) that has to run
In my django project, I have 4 models that are all tied together in
I have a Django project, that has a Address model. This is used in
I've got a Django project that has multiple settings files (www site, mobile site,

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.