Django机型:只允许一个模型中的一个条目? [英] Django models: Only permit one entry in a model?
问题描述
为此,我决定将它们设置为数据库字段,而不是比 settings.py
。
这些是我关心的设置:
class ManagementEmail(models.Model):
librarian_email = models.EmailField()
intro_text = models.CharField(max_length = 1000)
signoff_text = models.CharField(max_length = 1000)
这些是一次性全局设置,所以我只想有一个单独的 librarian_email
, intro_text
等浮动在系统周围。
有没有办法阻止管理员用户在这里添加新记录而不阻止他们编辑现有记录?
我想我可以通过为此模型编写自定义管理模板来实现,但我想知道是否有更好的方式来配置这个。
可以使用除 class
以外的其他东西吗?
谢谢! / p>
请参阅关于保持数据库中的设置的问题,其中答案似乎是 django-dbsettings
更新
只想到另一个选项:您可以创建跟随模型:
from django.contrib.sites.models import Site
class ManagementEmail(models。模型):
site = models.OneToOneField(Site)
librarian_email = models.EmailField()
intro_text = models.CharField(max_length = 1000)
signoff_text = models.CharField max_length = 1000)
由于 OneToOneField
字段,每个站点只能有一个ManagementEmail记录。然后,只需确保您使用的是网站,然后您就可以从django.contrib.sites中获取设置:
模型导入站点
managementemail = Site.objects.get_current()。managementemail
请注意,什么其他人都告诉你是真的如果您的目标是存储设置,则将它们逐个添加到模型的字段不是最佳实现。随着时间的推移添加设置将会令您感到头痛:您必须将字段添加到模型中,更新数据库结构,并修改调用该设置的代码。
I want to make some of my Django global settings configurable through the admin interface.
To that end, I've decided to set them as database fields, rather than in settings.py
.
These are the settings I care about:
class ManagementEmail(models.Model):
librarian_email = models.EmailField()
intro_text = models.CharField(max_length=1000)
signoff_text = models.CharField(max_length=1000)
These are one-off global settings, so I only ever want there to be a single librarian_email
, intro_text
etc floating around the system.
Is there a way I can prevent admin users from adding new records here, without preventing them from editing the existing record?
I guess I can do this by writing a custom admin template for this model, but I'd like to know if there's a neater way to configure this.
Could I use something other than class
, for example?
Thanks!
Please see this question on "keep[ing] settings in database", where the answer seems to be django-dbsettings
Update
Just thought of another option: you can create the following model:
from django.contrib.sites.models import Site
class ManagementEmail(models.Model):
site = models.OneToOneField(Site)
librarian_email = models.EmailField()
intro_text = models.CharField(max_length=1000)
signoff_text = models.CharField(max_length=1000)
Because of the OneToOneField
field, you can only have one ManagementEmail record per site. Then, just make sure you're using sites and then you can pull the settings thusly:
from django.contrib.sites.models import Site
managementemail = Site.objects.get_current().managementemail
Note that what everyone else is telling you is true; if your goal is to store settings, adding them one by one as fields to a model is not the best implementation. Adding settings over time is going to be a headache: you have to add the field to your model, update the database structure, and modify the code that is calling that setting.
That's why I'd recommend using the django app I mentioned above, since it does exactly what you want -- provide for user-editable settings -- without making you do any extra, unnecessary work.
这篇关于Django机型:只允许一个模型中的一个条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!