Django如何添加外键 - 多种选择

时间:2015-05-13 15:52:36

标签: python django django-models

您好我创建了两个型号,第二个必须首先使用它,例如第一个型号是产品,第二个是包含很少产品的商店。

from django.db import models

# Create your models here.


class Product(models.Model):
    name = models.CharField("name", max_length=40)
    price = models.FloatField("price")

    def __unicode__(self):
        return self.name

class Shop(models.Model):
    product = models.ForeignKey(Product, null = True)
    name = models.CharField("shopname", max_length=40)
    salary = models.FloatField("salary")

    def __unicode__(self):
        return self.name

这是一个好的开始吗?我想创建一些东西,让我有可能将许多Product.models添加到Shop.model。如何创建它?

第二个问题是 - 现在我点击

  

python manage.py迁移homebudget

我有信息

   (venv) C:\Users\noname\nowe\budget>python manage.py makemigrations homebudget
Migrations for 'homebudget':
  0011_auto_20150513_1817.py:
    - Alter field price on product
    - Alter field name on product
    - Alter field name on shop
    - Alter field salary on shop

(venv) C:\Users\noname\nowe\budget>python manage.py migrate homebudget
Operations to perform:
  Apply all migrations: homebudget
Running migrations:
  Rendering model states... DONE
  Applying homebudget.0002_shop_product...Traceback (most recent call las
t):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\noname\nowe\venv\lib\site-packages\django\core\management\__init_
_.py", line 338, in execute_from_command_line
    utility.execute()
  File "C:\Users\noname\nowe\venv\lib\site-packages\django\core\management\__init_
_.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\noname\nowe\venv\lib\site-packages\django\core\management\base.py
", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\noname\nowe\venv\lib\site-packages\django\core\management\base.py
", line 441, in execute
    output = self.handle(*args, **options)
  File "C:\Users\noname\nowe\venv\lib\site-packages\django\core\management\command
s\migrate.py", line 221, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "C:\Users\noname\nowe\venv\lib\site-packages\django\db\migrations\executor.
py", line 110, in migrate
    self.apply_migration(states[migration], migration, fake=fake, fake_initial=f
ake_initial)
  File "C:\Users\noname\nowe\venv\lib\site-packages\django\db\migrations\executor.
py", line 147, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\noname\nowe\venv\lib\site-packages\django\db\migrations\migration
.py", line 115, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, projec
t_state)
  File "C:\Users\noname\nowe\venv\lib\site-packages\django\db\migrations\operation
s\fields.py", line 62, in database_forwards
    field,
  File "C:\Users\noname\nowe\venv\lib\site-packages\django\db\backends\sqlite3\sch
ema.py", line 176, in add_field
    self._remake_table(model, create_fields=[field])
  File "C:\Users\noname\nowe\venv\lib\site-packages\django\db\backends\sqlite3\sch
ema.py", line 74, in _remake_table
    self.effective_default(field)
  File "C:\Users\noname\nowe\venv\lib\site-packages\django\db\backends\base\schema
.py", line 194, in effective_default
    default = field.get_default()
  File "C:\Users\noname\nowe\venv\lib\site-packages\django\db\models\fields\relate
d.py", line 1930, in get_default
    if isinstance(field_default, self.rel.to):
TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and typ
es

(venv) C:\Users\noname\nowe\budget>

3 个答案:

答案 0 :(得分:0)

运行此代码并没有给我任何错误,但看起来nofinator有一个点,字段调用不希望名称作为位置参数。如果你想要它们,最好使用verbose_name例如,

salary = models.FloatField(verbose_name="salary")

但是,除非你用它来提供一个更易读的名字,否则它没有多大意义。

答案 1 :(得分:0)

我认为在定义外键时需要'Product'左右的引号:

product = models.ForeignKey('Product', null = True)
  

如果您需要在尚未使用的模型上创建关系   定义后,您可以使用模型的名称,而不是模型   对象本身:

https://docs.djangoproject.com/en/1.8/ref/models/fields/#django.db.models.ForeignKey

答案 2 :(得分:0)

您可能希望使用models.ManyToManyField而不是外键。你现在写的方式是Product可以有很多Shop个。

至少你希望Shop有很多Product个;在这种情况下,在这种情况下ForeginKey中有Product

你需要写verbose_name="salary"等来摆脱这个问题。