Django:构造Django模型以允许任意字段类型
我想在Django中创建一个用户配置文件应用程序(我知道存在一些,谢谢),我想知道您将如何构造模型,以便在每个小节中允许任意组合字段 例如,“教育”部分可能有一个子部分称为“编程经验”,而“个人信息”部分可能有一个子部分称为“收藏夹” 考虑到典型的侧栏导航设置,每个部分都是一个标题,每个子部分都是一个链接,指向一个可以操纵信息的表单Django:构造Django模型以允许任意字段类型,django,django-models,Django,Django Models,我想在Django中创建一个用户配置文件应用程序(我知道存在一些,谢谢),我想知道您将如何构造模型,以便在每个小节中允许任意组合字段 例如,“教育”部分可能有一个子部分称为“编程经验”,而“个人信息”部分可能有一个子部分称为“收藏夹” 考虑到典型的侧栏导航设置,每个部分都是一个标题,每个子部分都是一个链接,指向一个可以操纵信息的表单 Education - Schooling - Programming Experience Personal Info - Location - Favouri
Education
- Schooling
- Programming Experience
Personal Info
- Location
- Favourites
- Look a-likes
我想做的是能够在任意的基础上向子部分添加项目。无论网站的感觉如何
也许一个网站可以从用户上过的学校的照片中获益,而另一个网站可能只需要一个描述
我想使用管理界面将这些字段类型添加到子部分。因此,添加一个项目将显示它是什么类型的信息(图像、视频、文本等)以及它将应用于什么子部分的选择
我想知道你将如何做到这一点;更重要的是,通过尽可能少的跳圈
谢谢
编辑:
为了澄清这个问题,我将提供一个示例models.py文件。这只是一个快速的moch up,以更准确地演示问题。我心中有两个解决方案,我认为解决方案二比解决方案一更有效;但我也想在这里谈谈SO社区的想法,以及他们是否有自己的其他解决方案
**models.py**
class Section(models.Model):
"""
The root of categorization. Acts much like a header
"""
name = models.CharField(max_length=30)
description = models.CharField(max_length=255)
class SubSection(models.Model):
"""
The contents of each section. Contains many items of varying types as needed
by the site developer.
"""
name = models.CharField(max_length=30)
description = models.CharField(max_length=255)
section = models.ForeignKey(Section)
class Item(models.Model):
"""
I would like this to store the information here and have a foreign key to the
'SubSection' table. The problem is that there are a lot of different information
types that can be stored and I'd need a row for each type. Thus for each
entry most of the columns will be blank.
I'm thinking that it may be better to use this table as a pointer to another
table that actually contains the information. This will result in a lot of
tables but will eliminate waste.
"""
name = models.CharField(max_length=30)
description = models.CharField(max_length=255)
sub_section = models.ForeignKey(SubSection)
### Solution One
# Storing the info here results in a lot of wasted space and may not be all
# that flexible
image = models.ImageField()
text = models.CharField(max_length=255)
numeric = models.IntegerField()
time = models.TimeField()
# etc ...
### Solution Two
# Storing the field info results in more tables but allows for a better match
# of information and field type.
field_type = models.CharField(max_length=255)
field_properties = models.CommaSeparatedIntegerField(max_length=None)
### Solution Two Tables
# Solution two would require a table for each field type supported here, which
# is quite a few different types.
class ImageStorage(models.Model):
item = models.ForeignKey(Item)
information = models.ImageField()
class IntegerStorage(models.Model):
item = models.ForeignKey(Item)
information = models.IntegerField()
### etc ...
请记住,它是针对用户配置文件的。因此,减肥网站可能希望用户在个人资料中的当前体重(数字信息),而旅游网站可能希望访问的地点列表(文本信息,我想甚至可以使用IPAddressField)。我只是不知道会弹出什么,所以我正在尝试使它尽可能通用。如果我正确地理解你,最简单的方法可能是多对一关系。我不确定你是想按用户还是按站点定制这些,所以我假设你想按站点定制(切换很容易) 创建一个如下所示的表:
class Section(models.Model):
section = models.CharField()
sub-section = model.CharField()
site = models.ForeignKey(Site)
对于属于同一节的多个子节,只需为它们指定相同的主节名称,以便您可以使用该名称在DB中查询伴随的子节
另一种方法是使用两个表来完成相同的事情,但考虑到应用程序,我认为这可能更合适