简体   繁体   English

使用Django存储/检索/编辑项目特定数据

[英]Storing/Retrieving/Editing project specific data with Django


I'm new to Django and I'm working on the public website for a small company. 我是Django的新手,并且正在一家小型公司的公共网站上工作。 I'm facing an issue that I guess has already been encountered by lots a django noobs, but I can't manage to find a good solution. 我面临的一个问题是,很多Django初学者已经遇到了这个问题,但是我无法找到一个好的解决方案。 My problem is that there some informations (contact address, office phone number, company description...) that I use in nearly all of my views and are by nature unique (undertand: a database table with only 1 row). 我的问题是,我几乎在所有视图中都使用了一些信息(联系地址,办公室电话号码,公司说明...),并且这些信息本质上是唯一的(理解是:只有1行的数据库表)。 I currently store these informations has a model in my databse, but I find it a bit weird issue an additional database request each time (each view) I need to access them. 我目前在数据库中存储了这些信息的模型,但是我发现每次访问每个视图时(每个视图)都会发出一个额外的数据库请求,这有点奇怪。 However, I need my client to be able to edit these informations (by the admin interface). 但是,我需要我的客户端能够(通过管理界面)编辑这些信息。 So, please, is there a django idiom to handle such an use case ? 那么,请问,有没有处理这种用例的django习惯用法? Thx in advance. 提前谢谢。

If you look into caching solutions, they will probably do what you need. 如果您研究缓存解决方案,它们可能会满足您的需求。

The general queryset caching solution I use in johnny-cache, but for what you need, you can probably just load it up from the db and store it in the cache. 我在johnny-cache中使用了通用的queryset缓存解决方案,但是对于您所需要的,您可以仅从db加载它并将其存储在缓存中。

What you want to do is use select_related('contact_profile','office_data') etc when you query the items in your view, and in the admin, instead of registering all the data separately just use the InlineAdmin class for the Admin site and you will be able to edit all the information as if it was a single entity. 当您在视图中以及在admin中查询项目时,您想要做的是使用select_related('contact_profile','office_data')等,而不是分别注册所有数据,只需对Admin网站使用InlineAdmin类,您就可以将能够像编辑单个实体一样编辑所有信息。

Check out the django docs for more information. 查看django文档以获取更多信息。

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

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