Django在错误的数据库中搜索表

时间:2014-04-25 10:59:50

标签: python mysql django django-admin

Settings.py:

    DATABASES = {
       'default': {
          'ENGINE': 'django.db.backends.mysql',
          'NAME': 'leavebuddy_master',
          'USER': 'master___user',
          'PASSWORD': '******',
          'HOST': '/Applications/MAMP/tmp/mysql/mysql.sock',
          'PORT': '3306',
},
   'company': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'leavebuddy_company',
    'USER': 'leavebuddy_user2',
    'PASSWORD': 'cork10[lad',
    'HOST': '/Applications/MAMP/tmp/mysql/mysql.sock',
    #'HOST': '127.0.0.1',
    'PORT': '3306',
}

}

Models.py 1st :(对于默认数据库):

    from django.db import models

    class Company(models.Model):
         ........

Models.py第二名:(对于公司数据库):

    from django.db import models

    class CompanyInfo(models.Model):
         ...........
    class Employee(models.Model):
         .............

Admin.py:

  from django.contrib import admin
  from leavebuddymaster.models import Company
  from leavebuddyapp.models import Employee

  class MultiDBModelAdmin(admin.ModelAdmin):
       using = 'company'

      def save_model(self, request, obj, form, change):
           # Tell Django to save objects to the 'other' database.
           obj.save(using=self.using)

      def delete_model(self, request, obj):
           # Tell Django to delete objects from the 'other' database
           obj.delete(using=self.using)

      def get_queryset(self, request):
          # Tell Django to look for objects on the 'other' database.
          return super(MultiDBModelAdmin, self).get_queryset(request).using(self.using)

      def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
          # Tell Django to populate ForeignKey widgets using a query
          # on the 'other' database.
          return super(MultiDBModelAdmin, self).formfield_for_foreignkey(db_field,     request=request, using=self.using, **kwargs)

     def formfield_for_manytomany(self, db_field, request=None, **kwargs):
          # Tell Django to populate ManyToMany widgets using a query
          # on the 'other' database.
          return super(MultiDBModelAdmin, self).formfield_for_manytomany(db_field, request=request, using=self.using, **kwargs)

     class EmployeeAdmin(MultiDBModelAdmin):
           list_display = ('id', 'name', 'company')

     admin.site.register(Employee, EmployeeAdmin)

但是现在当我尝试通过管理网站添加员工时,它会给我一个错误

     (1146, "Table 'leavebuddy_master.leavebuddyapp_employee' doesn't exist")

而是应该在“公司”数据库中搜索员工表。你们能指导我正确的方向吗?提前谢谢。

0 个答案:

没有答案