django 用户模型外键问题
我在 django 中有一个简单的 userprofile 类,
class Profile(models.Model):
user = models.OneToOneField(User,unique=True)
gender = models.IntegerField(blank=True, default=0, choices=UserGender.USER_GENDER,db_column='usr_gender')
education = models.IntegerField(blank=True, default=0, choices=UserEducation.USER_EDU,db_column='usr_education')
mail_preference = models.IntegerField(blank=True, default=1, choices=UserMailPreference.USER_MAIL_PREF,db_column='usr_mail_preference')
birthyear = models.IntegerField(blank=True, default=0,db_column='usr_birthyear')
createdate = models.DateTimeField(default=datetime.datetime.now)
updatedate = models.DateTimeField(default=datetime.datetime.now)
deletedate = models.DateTimeField(blank=True,null=True)
updatedBy = models.ForeignKey(User,unique=False,null=True, related_name='%(class)s_user_update')
deleteBy = models.ForeignKey(User,unique=False,null=True, related_name='%(class)s_user_delete')
activation_key = models.CharField(max_length=40)
key_expires = models.DateTimeField()
您可以看到 deletedBy
和 updatedBy
是用户类的外键字段。如果我不写 lated_name='%(class)s_user_update'
它会给我错误(我不知道为什么)。 尽管此操作没有任何错误,但它不会推送 deletedBy
和 updatedBy
字段的用户 ID,尽管我为它们分配了正确的用户。
能给我任何想法并解释 lated_name='%(class)s_user_update'
部分吗?
谢谢
I have a simple userprofile class in django such that
class Profile(models.Model):
user = models.OneToOneField(User,unique=True)
gender = models.IntegerField(blank=True, default=0, choices=UserGender.USER_GENDER,db_column='usr_gender')
education = models.IntegerField(blank=True, default=0, choices=UserEducation.USER_EDU,db_column='usr_education')
mail_preference = models.IntegerField(blank=True, default=1, choices=UserMailPreference.USER_MAIL_PREF,db_column='usr_mail_preference')
birthyear = models.IntegerField(blank=True, default=0,db_column='usr_birthyear')
createdate = models.DateTimeField(default=datetime.datetime.now)
updatedate = models.DateTimeField(default=datetime.datetime.now)
deletedate = models.DateTimeField(blank=True,null=True)
updatedBy = models.ForeignKey(User,unique=False,null=True, related_name='%(class)s_user_update')
deleteBy = models.ForeignKey(User,unique=False,null=True, related_name='%(class)s_user_delete')
activation_key = models.CharField(max_length=40)
key_expires = models.DateTimeField()
You can see that deletedBy
and updatedBy
are foreign key fields to user class. If I don't write related_name='%(class)s_user_update'
it gives me error (I don't know why).
Although this works without any error, it doesn't push the user id's of deletedBy
and updatedBy
fields although I assign proper user to them.
Could give me any idea and explain the related_name='%(class)s_user_update'
part ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
'%(class)s_user_update'
暗示它是一个等待格式化的字符串。您通常会在上下文中看到它:它将变成:
您可以在 python 文档中阅读有关 python 字符串格式的更多信息。 字符串格式化操作
我看不出你的代码会如何工作:也许你想要:
如果它确实工作,那是因为 django 在幕后做了一些奇特的魔术,并用当前类的类名替换
'%(class)s'
。上述注释:
models.py
文件中。这是您需要担心的少一个导入,特别是如果您不在models.py
文件中的任何位置使用User
类。'%(class)s_user_update'
implies that it is a string awaiting formatting. You would normally see it in the context:Which would become:
You can read more about python string formatting in the python docs. String Formatting Operations
I can't see how the code you have would ever work: perhaps you want:
If it does work, it is because django is doing some fancy magic behind the scenes, and replacing
'%(class)s'
by the class name of the current class.Notes on the above:
models.py
file. It is one less import you'll need to worry about, especially if you don't use theUser
class anywhere in yourmodels.py
file.您可以在此处阅读有关 related_name 内容的更多信息:
https: //docs.djangoproject.com/en/1.3/topics/db/queries/#following-relationships-backward
You can read more about the related_name stuff here:
https://docs.djangoproject.com/en/1.3/topics/db/queries/#following-relationships-backward