1 回答

TA貢獻1895條經(jīng)驗 獲得超3個贊
值得一提的是,您正在將int類型傳遞給string方法。這就是錯誤所指示的內(nèi)容。
使用randint將返回一個整數(shù),最適合此用例。一種方法是重寫模型保存方法:
from random import randint
class Product(models.Model):
title = models.CharField(max_length=255)
slug = models.SlugField(max_length=255)
category = models.ForeignKey(Category, related_name='products', on_delete=models.CASCADE)
product_id = models.IntegerField(null=True, Blank=True) # Max length 5, numerals only
description = models.TextField(blank=True, null=True)
price = models.FloatField()
def save(self, **kwargs):
if not self.product_id:
self.product_id = randint(10000, 99999)
return super(Product, self).save(**kwargs)
添加回答
舉報