하나는 외부 키 관계가있는 두 모델을 설정했습니다. 여러 데이터베이스가 있기 때문에 두 데이터베이스가 동일한 응용 프로그램에 있기 때문에 routers.py 파일도 설정합니다. 나는이 문서를 읽고이어서 따라 갔고 here과 here을 찾았고 나는 문서에 따라 편지를 쓰고 있다고 말할 수있다.Django 역방향 foreignkey 관계가 실패했습니다
class Customers(models.Model):
customer_id = models.AutoField(primary_key=True)
name = models.CharField(max_length=100)
active = models.IntegerField()
class Meta:
db_table = 'customers'
app_label = 'db1'
class Users(models.Model):
user_id = models.AutoField(primary_key=True)
customer = models.ForeignKey(Customers, models.CASCADE)
username = models.CharField(max_length=35)
role = models.ForeignKey(Roles, models.CASCADE)
active = models.IntegerField()
class Meta:
db_table = 'users'
app_label = 'db1'
routers.py
class BackendRouter(object):
def db_for_read(self, model, **hints):
if model._meta.app_label == 'db1':
return 'default'
elif model._meta.app_label == 'db2':
return 'db2'
return None
def db_for_write(self, model, **hints):
if model._meta.app_label == 'db1':
return 'default'
elif model._meta.app_label == 'db2':
return 'db2'
return None
def allow_relation(self, obj1, obj2, **hints):
if obj1._meta.app_label == 'db1' or \
obj2._meta.app_label == 'db1':
return True
elif obj1._meta.app_label == 'db2' or \
obj2._meta.app_label == 'db2':
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label == 'db1':
return db == 'default'
elif app_label == 'db2':
return db == 'db2'
return None
이 AbstractBaseUser
에서 파생없이 나는 users
테이블을 가지고 잠시 무시 models.py 나는 다음과 같은 쿼리를 실행할 때 :
문서에
django.core.exceptions.FieldError: Cannot resolve keyword 'users' into field. Choices are: active, customer_id, name
예 당 : 10
나는 다음과 같은 오류가
Blog.objects.filter(entry__headline__contains='Lennon')
이 작동합니다. 내가 여기서 무엇을 놓치고 있니?
Customers.objects.filter(users_set__username__contains=‘sl’)
뿐만 아니라 customers
외래 키에 related_name='users'
설정 : 기준점으로
나는 이미뿐만 아니라 다음과 같은 노력했다.
'사용자'클래스에'username' 필드가 없다는 것을 알고 있습니까? –
@ChristianTernus 죄송합니다. 나머지 걸레질을 삭제할 때 삭제해야합니다. 안심 하셔도 지금은 예제에 있고 내 데이터베이스에는 분명히 있습니다. – beardedeagle