你发布的代码有几个问题。
首先,你不能通过
is_admin
我是说,
is_superuser
和
is_staff
当您在中创建用户模型时
create_user()
是的。这些论点在
kwargs
应该这样通过:
def create_user(self, email, shop_name='', mall_name='', password=None, **kwargs):
if not email:
raise ValueError('You must enter an email address')
user = self.model(
email=self.normalize_email(email),
shop_name=shop_name,
mall_name=mall_name,
**kwargs # Pass the additional keyword arguments
)
...
第二,在
save()
方法
Shop
,你只是打电话
保存()
在没有弹头的超类上对…的呼唤
super(Shop, self).save()
可能总是会发生-例如。
def save(self, *args, **kwargs):
try:
if not self.slug:
self.slug = slugify(self.shop_name + " shop " + get_random_string(20) )
super(Shop, self).save(*args, **kwargs) # Always call the save in the superclass
#add shop_name with 10 randomly generated characters to create slug
except IntegrityError:
pass # Bad idea to swallow exceptions.
悄悄地忽略
IntegrityError
是的。最好完全放弃try/except,或者至少记录错误。