为什么会收到TypeError:“ vc”是此函数错误的无效关键字参数?

时间:2019-01-30 22:25:28

标签: django django-models

我不明白我在这里做错了。这是我的Django模型:

class VMMigrationEvent(models.Model):
    created = models.DateTimeField(auto_now_add=True)  # DateTime because there may be multiple events in a day.
    dc = models.TextField(max_length=150),                 # data center
    destination_host = models.TextField(max_length=150),   # host migrated to.
    message = models.TextField(),                          # The message from virtual center.
    updated = models.DateTimeField(auto_now=True)  # DateTime because there may be multifple events in a day.
    user = models.TextField(max_length=150),       # The user logged into the virtual center that execute the migration.
    vc = models.TextField(max_length=150),         # virtual center
    vm = models.ForeignKey(VirtualMachine)         # The VirtualMachine record associated with this event.

然后从python控制台执行此操作:

>>> from cm.models import *
>>> dc='DCM01N-01'
>>> destination_host='auroravm2-1.example.com'
>>> message='my hovercraft is full of eels.'
>>> user='mister_gumby'
>>> vc='vca-001-s.example.com'
>>> vm='ads-108'
>>> vm_db_obj = VirtualMachine.objects.filter(name=vm).latest('create_date')
>>> vmme = VMMigrationEvent.objects.create(dc=dc, 
... destination_host=destination_host,
... message=message, user=user, vc=vc, vm=vm_db_obj)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/apps/man/man/env/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/apps/man/man/env/lib/python2.7/site-packages/django/db/models/query.py", line 392, in create
    obj = self.model(**kwargs)
  File "/apps/man/man/env/lib/python2.7/site-packages/django/db/models/base.py", line 573, in __init__
    raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
TypeError: 'vc' is an invalid keyword argument for this function

为什么vc无效?这是我模型中的一个字段吗?

1 个答案:

答案 0 :(得分:1)

在几个字段定义之后有逗号:vc,还有dcdestination_hostuser。这会将它们变成元组,不能用作模型中的字段。

删除那些逗号

(而且,您可能是说CharField而不是TextField。)