django 用户模型外键问题

发布于 2024-11-10 04:04:51 字数 1408 浏览 4 评论 0原文

我在 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()

您可以看到 deletedByupdatedBy 是用户类的外键字段。如果我不写 lated_name='%(class)s_user_update' 它会给我错误(我不知道为什么)。 尽管此操作没有任何错误,但它不会推送 deletedByupdatedBy 字段的用户 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌能量女王 2024-11-17 04:04:51

'%(class)s_user_update' 暗示它是一个等待格式化的字符串。您通常会在上下文中看到它:

'%(foo)s other' % {'foo': 'BARGH'} 

它将变成:

'BARGH other'

您可以在 python 文档中阅读有关 python 字符串格式的更多信息。 字符串格式化操作

我看不出你的代码会如何工作:也许你想要:

class Profile(models.Model):
    # other attributes here
    updated_by = models.ForeignKey('auth.User', null=True, related_name='profile_user_update')
    deleted_by = models.ForeignKey('auth.User', null=True, related_name='profile_user_deleted')
    # other attributes here

如果它确实工作,那是因为 django 在幕后做了一些奇特的魔术,并用当前类的类名替换 '%(class)s'

上述注释:

  • 一致使用 *snake_case* 作为属性。如果必须使用驼峰式命名法,则所有变量应保持一致。特别是不要混合使用 *snake_case*、camelCaserunwordstogethersoyoucanttellwhereonestartsandtheotherends
  • 如果您有两个属性引用相同的外键,则必须告诉 ORM 哪个属性对应于反向关系。在这种情况下,两者都默认为“profile_set”,这会给您带来验证错误。
  • 使用“auth.User”而不是将 User 导入到 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:

'%(foo)s other' % {'foo': 'BARGH'} 

Which would become:

'BARGH other'

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:

class Profile(models.Model):
    # other attributes here
    updated_by = models.ForeignKey('auth.User', null=True, related_name='profile_user_update')
    deleted_by = models.ForeignKey('auth.User', null=True, related_name='profile_user_deleted')
    # other attributes here

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:

  • The consistent use of *snake_case* for attributes. If you must use camelCase, then be consistent for all variables. Especially don't mix *snake_case*, camelCase and runwordstogethersoyoucanttellwhereonestartsandtheotherends.
  • Where you have two attributes that reference the same Foreign Key, you must tell the ORM which one is which for the reverse relation. It will default to 'profile_set' in this case for both, which will give you the validation error.
  • Use 'auth.User' instead of importing User into the models.py file. It is one less import you'll need to worry about, especially if you don't use the User class anywhere in your models.py file.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文