English 中文(简体)
Django:如何设计以下模式?
原标题:Django: how to design the following model?

我想知道我如何设计我的决哥模型 以实现以下目标:

 Road -> Category (required): Highway (select list)

 Road -> Attribute (optional): Traffic -> Heavy + Moderate (checkboxes)

 Road -> Attribute (optional): Condition -> Smooth + Rough + Average(checkboxes)

将人口贩运、文化条件、文化条件等纳入道路类, 与为每组选择设立单独的类别, 与创建通用属性类, 是否合理?

我如何将选择显示为复选框?

这一模式的最终目标是能够创建查询,如“无交通顺畅的高速公路”等查询。

以下是我的尝试:

class Category(models.Model):

    CATEGORY_CHOICES = (
        ( highway ,  Highway ),
        ( parkway ,  Parkway ),
    )
    name = models.CharField(max_length=1, choices=CATEGORY_CHOICES, blank=False)

class Road(models.Model):

        name = models.TextField(blank=False)

        TRAFFIC_CHOICES = (
            ( moderate ,  Moderate ),
            ( busy ,  Busy ),
        )
        traffic = models.CharField(max_length=1, choices=TRAFFIC_CHOICES)

        CONDITION_CHOICES = (
            ( smooth ,  Smooth ),
            ( rough ,  Rough ),
            ( average ,  Average ),
        )
        condition = models.CharField(max_length=1, choices=CONDITION_CHOICES)
最佳回答

首先,将第一个 models.Text Field 改为 Char Field ,与其他模式一样。

类别不必是单独的模式,除非您打算在申请完成后添加新的类别,在这种情况下,它必须是一个单独的模式,而且您应该使用从公路到该类别的外国钥匙关系,并抛弃CATEGORY_CHOICES。

假设您从未打算添加新的类别, 您可以完全删除分类模型, 并将 CATEGORY_ CHOICES 放到路面 。 然后将名称改为 = 类别 = 并放到路面 。

所有这些字段的最长长度为1,这很好,但在这种情况下,你需要将CHOICES地图变成单字符,以便适合字段。例如:

CATEGORY_CHOICES = (
    ( H ,  Highway ),
    ( P ,  Parkway ),
)

您为什么想要使用复选框来选择交通和状况? 复选框意味着您可以选择多个答案, 而不是被迫只选择一个。 这在概念上对道路状况或交通没有意义, 而且它与 Char Field 不符( 因为 Char Field 只能存储一个值, 而不设置一些非常巧妙的设置 ) 。 您可以保留您拥有的系统, 并使用无线电按钮, 如果您更喜欢它们而不是下调, 但是您可以使用复选框, 而不删除这些选项, 而不是让每个复选框都有可能选择自己的 Boolean Field 或 Null Boolean Field 。

我通常将自己的选择置于阶级定义之外。 我不知道它是否有效, 当它在里面的时候,你期望它如何运作。 我将把它们移出阶级定义之外; 这也许不需要, 所以可以随意进行实验。

要总结(和我将 name 字段更改为 Char Field ,因为 Text Field 只针对真正的大块块,而不针对名称):

CATEGORY_CHOICES = (
    ( H ,  Highway ),
    ( P ,  Parkway ),
)
TRAFFIC_CHOICES = (
    ( M ,  Moderate ),
    ( B ,  Busy ),
)
CONDITION_CHOICES = (
    ( S ,  Smooth ),
    ( R ,  Rough ),
    ( V ,  Varying ),
)

class Road(models.Model):
    name = models.CharField(max_length=512, blank=False)
    category = models.CharField(max_length=1, choices=CATEGORY_CHOICES, blank=False)
    traffic = models.CharField(max_length=1, choices=TRAFFIC_CHOICES)
    condition = models.CharField(max_length=1, choices=CONDITION_CHOICES)

编辑 : 如果您真的确定复选框是最适合的, 那么您有两个主要选择。 如上所示, 如果您打算在应用程序完成后稍后添加选项, 那么您的策略就会不同( 您需要使用许多 Toy 字段) 。 否则您可以执行这些选项 :

class Road(models.Model):
    name = models.CharField(max_length=512, blank=False)
    category = models.CharField(max_length=1, choices=CATEGORY_CHOICES, blank=False)
    moderate_traffic = models.NullBooleanField()
    heavy_traffic = models.NullBooleanField()
    smooth_condition = models.NullBooleanField()
    rough_condition = models.NullBooleanField()
    varying_condition = models.NullBooleanField()

部分显示它们为组合复选框, 具体发生在您重新显示您的窗体时, 而不是在模型中 。

您也可以使用某种位元字段,但默认情况下未包括在决哥中的位元字段 -- -- 您必须安装扩展名。

问题回答

暂无回答




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签