我在Django中有一个模型configuration
,并希望用author
填充get_username
字段
这可以在模型中完成还是必须从表单中完成?如果必须在表单上,我该如何更改标准管理页面才能拥有此功能?
目前,该模型如下:
class Configuration(models.Model):
title = models.CharField(max_length=100,unique=True,blank=False)
author = models.CharField(max_length=50,blank=False)
created = models.DateTimeField("date created",auto_now_add=True)
modified = models.DateTimeField("date modified",auto_now=True)
description = models.CharField(max_length=512)
drawing = models.ForeignKey(Drawing)
instruments = models.ManyToManyField(Instrument)
def __unicode__(self):
return self.title
答案 0 :(得分:3)
使用models.ForeignKey
:
#models.py
from django.contrib.auth.models import User
class Configuration(models.Model):
author = models.ForeignKey(User)
...
#admin.py:
class Configuration_admin(admin.ModelAdmin):
fields = ('title', 'author',....)
答案 1 :(得分:0)
类似的东西:
from django.contrib.auth.models import User
class ...
...
username = models.ForeignKey(User)
答案 2 :(得分:0)
如果您想在模型和默认用户模型之间建立某种关系,那么您可以将用户模型扩展到您自己的自定义模型中,如下所示:
models.py
from django.contrib.auth.models import User
class Configuration(models.Model):
author = models.OneToOneField(User)
..
..