简体   繁体   English

django模型字段集合

[英]django collection of model fields

I am trying to setup different models in django. 我正在尝试在Django中设置不同的模型。

Some of my models includes fields for a text. 我的某些模型包含文本字段。 A text is defined by: - CharField (tex) - CharField (font-size) - CharField (font-weight) - CharField (color) 文本由以下项定义:-CharField(tex)-CharField(字体大小)-CharField(字体粗细)-CharField(颜色)

So some of my models need one to n of these texts. 因此,我的某些模型需要这些文本中的一个到n个。

Is it possible to create a collection of Fields, for example "Test-Collection" that includes all 4 fields. 是否可以创建字段的集合,例如包含所有4个字段的“ Test-Collection”。 So that i didn't have to write all 4 field manually for each text i need in a model? 这样我就不必为模型中需要的每个文本手动编写所有4个字段?

Some thing like that: 像这样的东西:

class Box(CMSPlugin):
    text1 = models.CharField(max_length=100)
    text1_font_weight = models.CharField(max_length=100)
    text1_font_size = models.CharField(max_length=100)
    text1_color = models.CharField(max_length=100)

    text2 = models.CharField(max_length=100)
    text2_font_weight = models.CharField(max_length=100)
    text2_font_size = models.CharField(max_length=100)
    text2_color = models.CharField(max_length=100)

    text3 = models.CharField(max_length=100)
    text3_font_weight = models.CharField(max_length=100)
    text3_font_size = models.CharField(max_length=100)
    text3_color = models.CharField(max_length=100)

Into that: 变成:

class Box(CMSPlugin):
    text1 = TextColelction...
    text2 = TextColelction...
    text3 = TextColelction... 

Maybe its better to define a separate model for text and have a ForeignKey relation to the Box. 为文本定义一个单独的模型并与Box具有ForeignKey关系可能更好。 For example: 例如:

class Text(models.Model):
    text = models.CharField(max_length=100)
    text_font_weight = models.CharField(max_length=100)
    text_font_size = models.CharField(max_length=100)
    text_color = models.CharField(max_length=100)

class Box(..):
   text1 = models.ForeignKey(Text)
   text2 = models.ForeignKey(Text)
   text3 = models.ForeignKey(Text)

I am not sure what you want to achieve. 我不确定您要实现什么。 It looks like you could simplify this model to this 看来您可以将此模型简化为

from django.db import models


class TextCollection(models.Model):
    text = models.CharField(max_length=100)
    text_font_weight = models.CharField(max_length=100)
    text_font_size = models.CharField(max_length=100)
    text_color = models.CharField(max_length=100)
    box = models.ForeignKey("Box", on_delete=models.CASCADE, related_name="textcollections") 


class Box(CMSPlugin):
    pass

This way you can use as much TextCollections as you want in a Box. 这样,您可以在Box中使用任意数量的TextCollection。 If you also want a Text in multiple Boxes you could use ManyToManyField instead of ForeignKey. 如果您还希望在多个框中输入文本,则可以使用ManyToManyField而不是ForeignKey。 https://docs.djangoproject.com/en/2.1/topics/db/examples/many_to_many/ https://docs.djangoproject.com/en/2.1/topics/db/examples/many_to_many/

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

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