Django model id变成一个元组 [英] Django model id turns into a tuple
问题描述
我有一个超级类,我的模型如下:
I have a super class for my models as below:
class BaseModel(models.Model):
""" BaseClass vase aksare model ha """
def __init__(self, *args, **kwargs):
super(BaseModel, self).__init__(args, kwargs)
print('******> base model __init__')
status = models.IntegerField(default=1)
is_deleted = models.BooleanField(default=False)
create_user = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_creator_related")
create_date = models.DateTimeField()
update_date = models.DateTimeField()
update_user = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_updater_related")
class Meta:
abstract = True
def validate(self):
print('^^^^^^^^^^^^^^^^^^^^^^^^^^^^base validation')
我有一个配置文件模型如下:
and I have a profile model as below:
class Profile(BaseModel):
def __init__(self, *args, **kwargs):
super(Profile, self).__init__(args, kwargs)
""" User profile """
user = models.OneToOneField(User, related_name='profile')
mobile = models.CharField(max_length=25, null=True)
firstname_en = models.CharField(max_length=500, null=True)
lastname_en = models.CharField(max_length=500, null=True)
gender = models.IntegerField(default=0)
birth_date = models.DateTimeField(null=True)
edu_bg = models.ForeignKey('Category', related_name="profile__edu_bg", null=True)
region = models.ForeignKey('Category', related_name="profile__region", null=True)
credit = models.DecimalField(default=0, decimal_places=6, max_digits=15)
key = models.TextField(null=True)
secret = models.TextField(null=True)
当我想插入一个新的userProfile如下: p $
I have an error when I want to insert a new userProfile as below:
TypeError:int()参数必须是字符串,像字节的对象或数字,而不是'tuple'。
TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'.
然后打印vars(userprofileObject),并意识到'id':((),{})
,但是我没有设置它。当我在插入代码中删除了 __ init __
函数或将ID设置为 None
时,问题解决了。
then print the vars(userprofileObject) and realized that 'id': ((), {})
, however, I have not set it. When I removed the __init__
functions or set id to None
in insertion code, problem solved.
任何想法?
我需要这些 __ init __
,也不想在我的代码中设置 id = None
I need those __init__
and also don't want to set id=None
in my code
推荐答案
这是django的模型工作。您不应该更改其 __ init __
方法。
这就是为什么
This is how django's models work. You shouldn't change their __init__
method.
This is why
您可能会试图通过覆盖
__ init __ $来自定义模型c $ c>方法。但是,如果这样做,请注意不要更改呼叫签名,因为任何更改可能会阻止模型实例的保存。而不是重写
__ init __
,请尝试使用以下方法之一:
You may be tempted to customize the model by overriding the
__init__
method. If you do so, however, take care not to change the calling signature as any change may prevent the model instance from being saved. Rather than overriding__init__
, try using one of these approaches:
# Add a classmethod on the model class:
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=100)
@classmethod
def create(cls, title):
book = cls(title=title)
# do something with the book
return book
book = Book.create("Pride and Prejudice")
源 https://docs.djangoproject.com/en/1.10/ref/models/instances/#django.db.models.Model
同时阅读此编写要在django中使用的__init__函数模型
这篇关于Django model id变成一个元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!