有没有办法扩展内置的Django Group对象以添加类似于扩展用户对象的方式的其他属性?使用用户对象,您可以执行以下操作:
class UserProfile(models.Model):
user = models.OneToOneField(User)
Run Code Online (Sandbox Code Playgroud)
并将以下内容添加到settings.py文件中
AUTH_PROFILE_MODULE = 'app.UserProfile'
Run Code Online (Sandbox Code Playgroud)
哪个让你:
profile = User.objects.get(id=1).get_profile()
Run Code Online (Sandbox Code Playgroud)
是否有任何相当于扩展组的方法?如果没有,我可以采取另一种方法吗?
我正在试图弄清楚如何在Django中创建自定义组,以便组与应用程序相关而不是与项目相关.
例如,当用户想要创建公司时,它应该是公司的所有者:
class Company(models.Model):
owner = models.ForeignKey(User)
name = models.CharField(max_length=64, unique=True)
description = models.TextField(max_length=512)
created_on = models.DateTimeField(auto_now_add=timezone.now)
class Meta:
permissions = (
("erp_view_company", "Can see the company information"),
("erp_edit_company", "Can edit the company information"),
("erp_delete_company", "Can delete the company"),
)
Run Code Online (Sandbox Code Playgroud)
# Create your views here.
def register(request, template_name='erp/register.html'):
if request.method == 'POST':
company_form = CompanyRegistrationForm(request.POST)
if company_form.is_valid():
# Create a new company object but avoid saving it yet
new_company = company_form.save(commit=False)
# Set the owner
if request.user.is_authenticated():
new_company.owner …Run Code Online (Sandbox Code Playgroud)