Python,Django:应用程序中的外键不起作用

时间:2017-09-27 17:53:19

标签: python django django-models

Python:3.6.2 Django:1.11.4

我们正在尝试跨应用程序使用foreignkey。地址由客户和代理消耗。我们也使用内联框架集。如果我将所有这些放在一个应用程序中,它工作正常,包括内联框架集。如果我分成多个应用程序,我有这个错误。请看下面的图片。

Foreignkey error

<BLOCKQUOTE>

Project folder structure

from django.db import models
#from apps.agent.models import Agent

class ContactInfo(models.Model):
    mobile_no = models.CharField(max_length=8)
    phone_no = models.CharField(max_length=10)
class Location(models.Model):
    location_name = models.CharField(max_length=50)
    city = models.CharField(max_length=20, blank=True, null=True)
    state = models.CharField(max_length=20, blank=True, null=True)
class Address(models.Model):
    #agent = models.Foreignkey(Agent)
    address1 = models.CharField(max_length=100)
    address2 = models.CharField(max_length=100)

apps(文件夹)
 代理人(app)
   models.py
  客户(app)
     models.py
  sharedmodels(app)
     models.py     

请注意以上文件夹结构的应用程序。

sharedmodels / models.py

from django.db import models
from apps.sharedmodels.models import Location, Address, ContactInfo

class Agent(models.Model):
    first_name = models.CharField(max_length=20)
    location = models.ManyToManyField(Location)
    address = models.Foreignkey(Address)
    contactinfo = models.OneToOneField(ContactInfo)

剂/ models.py

from django.db import models
#from apps.agent.models import Agent, Address
from apps.sharedmodels.models import ContactInfo, Address

class Customer(models.Model):
    first_name = models.CharField(max_length=10)
    #address = models.OneToOneField(Address)
    contactinfo = models.OneToOneField(ContactInfo)
    address = models.Foreignkey(Address)

客户/ models.py

# Application definition
INSTALLED_APPS = [
    'apps.customer',
    'apps.agent',
    'apps.sharedmodels',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

Settings.py - installapps部分

var optionMap = mutable.Map[String, Map[String, Array[String]]]()

1 个答案:

答案 0 :(得分:0)

将外键设置为字符串foreignkey

from django.db import models


class ContactInfo(models.Model):
    mobile_no = models.CharField(max_length=8)
    phone_no = models.CharField(max_length=10)


class Location(models.Model):
    location_name = models.CharField(max_length=50)
    city = models.CharField(max_length=20, blank=True, null=True)
    state = models.CharField(max_length=20, blank=True, null=True)


class Address(models.Model):
    agent = models.ForeignKey('agent.Agent', related_name='addresses')
    address1 = models.CharField(max_length=100)
    address2 = models.CharField(max_length=100)

sharedmodels / models.py

from django.db import models


class Agent(models.Model):
    first_name = models.CharField(max_length=20)
    location = models.ManyToManyField('sharedmodels.Location')
    address = models.ForeignKey('sharedmodels.Address', related_name='agents')
    contactinfo = models.OneToOneField('sharedmodels.ContactInfo')


from django.db import models

剂/ models.py

class Customer(models.Model):
    first_name = models.CharField(max_length=10)
    contactinfo = models.OneToOneField('sharedmodels.ContactInfo')
    address = models.ForeignKey('sharedmodels.Address')

客户/ models.py

{{1}}