简体   繁体   English

如何在Django中使用用户FK为模型指定默认值

[英]how to specify default values for model with user FK in django

I have looked at the questions for extending user fields in django and thought of using the FK to user method: 我研究了在django中扩展用户字段的问题,并考虑了将FK应用于用户方法:

class Parent(models.Model):
    user = models.ForeignKey(User)
    contact_means = models.IntegerField()
    parent_id = models.AutoField(primary_key = True)

I have a question here. 我在这里有一个问题。 With this method (compared to user inheritance), in the admin site, after i registered Parent, how do I specify default values for the USER attributes like 'is_active' and or hide USER fields in the edit page? 使用此方法(与用户继承相比),在管理站点中注册了Parent之后,如何为USER属性(例如“ is_active”)指定默认值,或者如何在编辑页面中隐藏USER字段? Can I even do that in Parent's ModelAdmin class ? 我什至可以在Parent的ModelAdmin类中做到这一点?

Thanks folks! 谢谢大家!

Can you clarify what you mean by specifying default attributes for user after registering Parent? 注册父母后,您可以通过为用户指定默认属性来阐明您的意思吗?

Are you creating the user object from your Parent object? 您是从父对象创建用户对象吗? I could help better if I understood the flow between your User object creation and Parent object creation. 如果我了解您的User对象创建和Parent对象创建之间的流程,我会更好地帮助您。

To hide your user field: specify the fields attribute. 隐藏用户字段:指定fields属性。 This assumes that user has already been set. 假设已经设置了用户。

class MyModelAdmin(admin.ModelAdmin):
    fields = ('contact_means',) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM