TypeError:'ForeignKey'对象不可调用

问题描述 投票:0回答:2
from django.db import models
from django.utils.translation import ugettext_lazy as _

class ContactInfo(models.Model):
        TYPES_CHOICES = (
            ('Phone', ('telephone')),
            ('whatsapp', ('whatsapp')),
            ('OTHER', ('Other'))
        )

        type = models.CharField(_('Type'), max_length=20, choices=TYPES_CHOICES)
        number = models.CharField(max_length=10)

        def __str__(self):
            return self.type


    class Address(models.Model):
        TYPES_CHOICES = (
            ('HOME', ('Home')),
            ('WORK', ('Work')),
            ('OTHER', ('Other'))
        )

        type = models.CharField(_('Type'), max_length=20, choices=TYPES_CHOICES)

        departement = models.CharField(_('Departement'), max_length=50, blank=True)
        corporation = models.CharField(_('Corporation'), max_length=100, blank=True)
        building = models.CharField(_('Building'), max_length=20, blank=True)
        floor = models.CharField(_('Floor'), max_length=20, blank=True)
        door = models.CharField(_('Door'), max_length=20, blank=True)
        number = models.CharField(_('Number'), max_length=30, blank=True)
        street_line1 = models.CharField(_('Address 1'), max_length=100, blank=True)
        street_line2 = models.CharField(_('Address 2'), max_length=100, blank=True)
        zipcode = models.CharField(_('ZIP code'), max_length=5, blank=True)
        city = models.CharField(_('City'), max_length=100, blank=True)
        state = models.CharField(_('State'), max_length=100, blank=True)
        country = models.CharField(_('Country'), max_length=100, blank=True)
        contact = models.ForeignKey(ContactInfo, on_delete=models.CASCADE, null=True, related_name='contact_info')

        def __str__(self):
            return self.zipcode

[我正在尝试运行makemigrations,但我收到此错误TypeError:'ForeignKey'对象不可调用,我也不知道为什么我现在一直在使用django一段时间,但也许我缺少了某些东西,任何想法我都错了?

这是回溯,并且在重新构建整个项目之后,我收到了相同的错误...

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\AnthonyDreams\desktop\env\waomoviesenv\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "C:\Users\AnthonyDreams\desktop\env\waomoviesenv\lib\site-packages\django\core\management\__init__.py", line 347, in execute
    django.setup()
  File "C:\Users\AnthonyDreams\desktop\env\waomoviesenv\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\AnthonyDreams\desktop\env\waomoviesenv\lib\site-packages\django\apps\registry.py", line 112, in populate
    app_config.import_models()
  File "C:\Users\AnthonyDreams\desktop\env\waomoviesenv\lib\site-packages\django\apps\config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Users\AnthonyDreams\desktop\env\waomoviesenv\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\AnthonyDreams\Desktop\projects\pacome\apps\foodtrucks\models.py", line 3, in <module>
    from apps.address.models import Address
  File "C:\Users\AnthonyDreams\Desktop\projects\pacome\apps\address\models.py", line 20, in <module>
    class Address(models.Model):
  File "C:\Users\AnthonyDreams\Desktop\projects\pacome\apps\address\models.py", line 41, in Address
    contact = models.ForeignKey(ContactInfo, on_delete=models.CASCADE, null=True, related_name='contact_info')
TypeError: 'ForeignKey' object is not callable
python django django-models foreign-keys
2个回答
0
投票

回答约旦M.并不是一个错误的参考问题,即使我在设置中注释#app.address,当尝试在服务模型中迁移horario foreingkey时,它也会再次发生,我不知道我错过了什么,但看起来像是胡说八道的错误就在这里

from django.db import models
from apps.products.models import Product
from apps.address.models import Address
# Create your models here.

class Horarios(models.Model):
    day_start = models.DateTimeField()
    day_end = models.DateTimeField()
    description = models.TextField(null=True, blank=True)


class Service(models.Model):
    name = models.CharField(max_length=250)
    about = models.CharField(max_length=800)
    scheduled = models.ForeignKey(Horarios, on_delete=models.CASCADE)
    products = models.ForeignKey(Product, on_delete=models.CASCADE)
    address = models.OneToOneField(Address, on_delete=models.CASCADE)

0
投票

此代码运行正常。我在本地计算机上运行了代码片段,所有模型都成功迁移。尝试删除由makemigrations cmd创建的迁移文件,然后再次尝试迁移。

© www.soinside.com 2019 - 2024. All rights reserved.