Python 另一个Django模型的Django模型最大长度
我有两个模型。 模型副本应从模型AdSpot获取最大长度,而不从其继承。 经过多次尝试,我没能成功:Python 另一个Django模型的Django模型最大长度,python,django,Python,Django,我有两个模型。 模型副本应从模型AdSpot获取最大长度,而不从其继承。 经过多次尝试,我没能成功: class AdSpot(models.Model): title_max_chars = models.IntegerField(default=0) @property def title_max_chars_from_adspot(self): return self.title_max_chars 错误是 polls.Copy.title:
class AdSpot(models.Model):
title_max_chars = models.IntegerField(default=0)
@property
def title_max_chars_from_adspot(self):
return self.title_max_chars
错误是
polls.Copy.title: (fields.E121) 'max_length' must be a positive integer.
我错过了什么?你在用错误的方式做事
max_length
应该是整数,并且您正在传递python
的FunctionType
对象title\u max\u chars\u from\u adspot
是Copy
类的属性
而且您没有Copy
类的实例(在makemigration/migration
过程中您永远不会得到它)。例如,唯一的方法是传递int
max_length=5
,或者为Copy
类写入@staticmethod
(但在这种情况下,无法访问ForeignKey
对象)。改变策略
class AdSpot(models.Model):
title_max_chars = models.IntegerField(default=22)
class Copy(models.Model):
adspot = models.ForeignKey(AdSpot, on_delete=models.PROTECT)
default_length = 255
for field in AdSpot._meta.fields:
if field.name == 'title_max_chars':
default_length = field.default
title = models.CharField('Name', max_length=default_length, default='') # default value of title_max_chars (22 this case) from AdSpot will be set as a max_length of title
你在用错误的方式做事
max_length
应该是整数,并且您正在传递python
的FunctionType
对象title\u max\u chars\u from\u adspot
是Copy
类的属性
而且您没有Copy
类的实例(在makemigration/migration
过程中您永远不会得到它)。例如,唯一的方法是传递int
max_length=5
,或者为Copy
类写入@staticmethod
(但在这种情况下,无法访问ForeignKey
对象)。改变策略
class AdSpot(models.Model):
title_max_chars = models.IntegerField(default=22)
class Copy(models.Model):
adspot = models.ForeignKey(AdSpot, on_delete=models.PROTECT)
default_length = 255
for field in AdSpot._meta.fields:
if field.name == 'title_max_chars':
default_length = field.default
title = models.CharField('Name', max_length=default_length, default='') # default value of title_max_chars (22 this case) from AdSpot will be set as a max_length of title
您正在将Adspot.title\u max\u chars设置为默认值等于0 但是,最大长度始终应为正整数。
尽量不要提供默认值。您正在将Adspot.title\u max\u chars设置为默认值等于0 但是,最大长度始终应为正整数。
尽量不要提供默认值。您可以使用
PositiveIntegerField
代替IntegerField
class AdSpot(models.Model):
title_max_chars = models.PositiveIntegerField(default=0)
@property
def title_max_chars_from_adspot(self):
return self.title_max_chars
您可以使用
PositiveIntegerField
代替IntegerField
class AdSpot(models.Model):
title_max_chars = models.PositiveIntegerField(default=0)
@property
def title_max_chars_from_adspot(self):
return self.title_max_chars
试试
max\u length=int(title\u max\u chars\u from\u adspot)
TypeError:int()参数必须是字符串、类似对象或数字的字节,而不是“函数”
Ok试试max\u length=title\u max\u chars\u from\u adspot(self)
您希望出现哪种行为?Copy
模型中的每一行必须具有不同的列title
宽度,这取决于链接的AdSpot
行?您不能在那里使用计算值,因为这是一个数据库约束,并且在应用迁移时它必须存在。您需要在save
方法上验证这一点。trymax\u length=int(title\u max\u chars\u from\u adspot)
TypeError:int()参数必须是字符串、类似字节的对象或数字,而不是“函数”
确定trymax\u length=title\u max\u chars\u from\u adspot(self)
您希望出现哪种行为?Copy
模型中的每一行必须具有不同的列title
宽度,这取决于链接的AdSpot
行?您不能在那里使用计算值,因为这是一个数据库约束,并且在应用迁移时它必须存在。您需要在save
方法上验证该值。Copy.title:(fields.E121)“max_length”必须是正整数。
Copy.title:(fields.E121)“max_length”必须是正整数。
谢谢,我做了一些初始测试,这是可行的,我做了一些初始测试,这是可行的