Previously i had confused how to handle this problem, this problem also already asked by someone on stackoverflow (Cross Database foreign key error) and has the best answered by someone with quote:

If you have used a router to partition models to different databases, any foreign key and many-to-many relationships defined by those models must be internal to a single database.
This is because of referential integrity. In order to maintain a relationship between two objects, Django needs to know that the primary key of the related object is valid. If the primary key is stored on a separate database, it’s not possible to easily evaluate the validity of a primary key.

yap, isn’t possible coz about has a different table.

Other solution by someone at the gist Django Custom Model ForeignKey Field for Spanning Databases with related ticket

I already try all of it. But still has an IntegrityError. I think, this problem maybe solve to create the new integer field that reference to the pk/id from other database (field). and thank you so much to god, i have been solved with this solution:

This screenshot bellow is sample result of it…

How to implement the ForeignKey for multi databases Django

1. Makesure you read first how to use the multi databases in Django with database router:

For example in this config below, i setup it with the postgres in my yourproject/

    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'db_2',
        'USER': 'db_user',
        'PASSWORD': 'db_password',
        'HOST': '',
        'PORT': '5432',
    'other_db': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'db_1',
        'USER': 'db_user',  
        'PASSWORD': 'db_password',  
        'HOST': '',
        'PORT': '5432',
DATABASE_ROUTERS = ['yourproject.routers.DBRouter']

2. Create new file example: yourproject/, please read more about it:

class DBRouter(object):
    A router to control all database operations on models in the
    auth application.
    $ ./ migrate
    $ ./ migrate --database=other_db  

    def db_for_read(self, model, **hints):
        Attempts to read auth models go to 'other_db'.  
        if model._meta.app_label == 'auth':
            return 'other_db'  
        return 'default'

    def db_for_write(self, model, **hints):
        Attempts to write auth models go to 'other_db'.  
        if model._meta.app_label == 'auth':
            return 'other_db'  
        return 'default'

    def allow_relation(self, obj1, obj2, **hints):
        Allow relations if a model in the auth app is involved.
        if obj1._meta.app_label == 'auth' or 
           obj2._meta.app_label == 'auth':
            return True
        return 'default'

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        Make sure the auth app only appears in the 'other_db'  

        if app_label == 'auth':
            return db == 'other_db'  
        return 'default'

3. This field user_id in the yourapp/ to save the id that refference to the another databases model (e.g: User)

from django.db import models
from django.contrib.auth.models import User

class Author(models.Model):
    user_id = models.PositiveIntegerField(
        help_text='Type username to search the user id.'
    date_approved = models.DateTimeField(auto_now_add=True)

    def get_user(self):
        return User.objects.get(pk=self.user_id)
    get_user.allow_tags = True
    get_user.short_description = 'user'

    def __str__(self):
        return self.get_user().username

Example for other model that having a ForeignKey to the Author.

class Post(models.Model):
    author = models.ForeignKey(Author, related_name='author_post')
    title = models.CharField(max_length=200)

4. For the next, i handle the user_id to can auto search the id by typing the username with EasyAutocomplete. but first, you need to rendering the json file before accessing EasyAutocomplete.

Example in this configuration allowing the permission only for superuser.

File: yourapp/

import json
from django.http import HttpResponse
from django.contrib.auth.decorators import user_passes_test
from django.contrib.auth.models import User

@user_passes_test(lambda u: u.is_superuser)
def json_users(request):
    Json authors by `username` & `pk/id`
    only allowed  for 'Developer/Superuser'

    data = [
            'username': user.username,
            'pk': str(
        } for user in User.objects.all()
    return HttpResponse(
        json.dumps(data, sort_keys=True),

5. And then, in yourapp/

from django.conf.urls import url
from yourapp.views import json_users

urlpatterns = [

6. This takken an easy if you need to render to the custom dashboard with, but if you work with default admin dashboard? you need to custom it.

File: yourapp/

from django.contrib import admin
from yourapp.models import Author

class AuthorAdmin(admin.ModelAdmin):
    list_display = ('get_user', 'date_approved')  
    list_filter = ['created']

    class Media:
        js = (
        css = {
            'all': (
        }, AuthorAdmin)

7. Create new file at /static/js/get_users.js to implement the EasyAutocomplete. and the id_user_id is the thml id from field user_id above.

$(document).ready(function() {
  var options = {
    url: "/admin/json_users.json",
    getValue: "username",
    template: {
      type: "description",
      fields: {
        description: "pk"
    list: {
      maxNumberOfElements: 100,
      match: {
        enabled: true
      onSelectItemEvent: function() {
        var index = $("#id_user_id").getSelectedItemData().pk;

Finally, spend a few cups of coffee to solved this problem :D Thank to Fahri Reza for sugested to use integer field.

101 1 1 0