【发布时间】:2025-11-29 15:00:01
【问题描述】:
我想创建模型对象来存储多个电话号码。我正在使用 CollectionField 但出现错误
File "/home/shiv/.local/lib/python3.7/site-packages/collectionfield/models/fields.py", line 226, in contribute_to_class
cls, name, virtual_only=virtual_only
TypeError: contribute_to_class() got an unexpected keyword argument 'virtual_only'
这是我的 models.py
from django.db import models
from multiselectfield import MultiSelectField
from django.utils import timezone
from collectionfield.models import CollectionField
class Applications(models.Model):
country = models.ForeignKey(Country, on_delete=models.SET_NULL, null=
state = models.ForeignKey(State, on_delete=models.SET_NULL, null=True)
city = models.ForeignKey(City, on_delete=models.SET_NULL, null=True)
zone = models.CharField(max_length=10, choices=zone_choice, blank=True)
name = models.CharField(max_length=20)
phone_number = CollectionField()
email_id = models.EmailField()
home_address = models.CharField(max_length=255, blank=True, null=True)
birthdate = models.DateField(null=True, blank=True)
def __str__(self):
return str(self.name)
【问题讨论】:
-
您遇到的错误是因为与 Django 2.0 及更高版本不兼容,其中删除了
Field.contribute_to_class()的 virtual_only 参数。 django-collection-field 自 2016 年以来没有更新 - 看起来没有维护。 -
django 1.8 如果要存储多个值该怎么办?
标签: django django-models django-forms