[英]How can I fill a field in Django with the username?
我在Django中有一個configuration
模型,並希望用get_username
填充author
字段。可以在模型中完成此操作還是必須從表單中完成? 如果必須在表單上,如何更改標准管理頁面以具有此功能? 目前,該模型的內容如下:
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
使用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',....)
像這樣的東西:
from django.contrib.auth.models import User
class ...
...
username = models.ForeignKey(User)
如果要在模型和默認用戶模型之間建立某種關系,則可以將用戶模型擴展到自己的自定義模型中,如下所示:
models.py
from django.contrib.auth.models import User
class Configuration(models.Model):
author = models.OneToOneField(User)
..
..
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.