假设我有一个三明治模型,我想说你想在三明治上放什么蛋白质:
class Sandwich(models.Model):
protein_choices = (
('n', 'Please Choose'),
('e1', 'Eggplant'),
('e2', 'Hummus'),
('v1', 'Provolone'),
('v2', 'Egg'),
('p1', 'Fish'),
('c1', 'Beef'),
('c2', 'Chicken'),
('c3', 'Pork'),
)
protein = models.CharField(
max_length=2,
choices=protein_choices,
default='n',
)
我如何做出按v E
gan,V
egetarian,P
escatarian或C
食肉动物分类的选择?
我希望能够检查其类别(三明治式素食主义者[假设类别不重叠]?),并且我一直在使用model.Manager,但我想确保所有选择都具有一个类别(我认为这是缺少的链接,并且不认为测试是正确的方法),仅选择了一个选项(已由显示的状态结构处理)。
应该以{{1}},与其他某种结构的1-> M关系还是通过model.Form
和其他方式来处理?
答案 0 :(得分:2)
另一个选项是内存中的数据结构和接口,可将您的状态键映射到其适当的类别。例如,您可以use an Enum with a category
property as the basis for your Choices.如果这些字符串之间的关系最终成为您要在数据库中管理的东西,则可以随时重构并添加迁移。
您已经注意到,正如其他人建议的那样,在模型中建立树形结构会导致大量联接。.在这种情况下,查看django-treebeard或{{3} },并检查是否有理由依赖该附加依赖项。
答案 1 :(得分:0)
我认为为蛋白质和类别建立单独的模型会很好。
示例:
class Protein(models.Model):
name = models.CharField(max_length=20)
class ProteinCategory(models.Model):
protein = models.ForeignKey(to="Protein")
name = models.CharField(max_length=20)
class Sandwich(models.Model):
protein = models.ForeignKey(to="Protein")
如果ManyToManyField
包含许多ForeignKey
,请在Sandwich
模型中使用Sandwich
代替Protein
。
如果OneToOneField
可以在一个ProteinCategory
中,并且只能在一个Protein
中,请在ProteinCategory
模型中使用akshay@db-3325:/var/www/html/pyrocms/system/cms$ grep -R encrypt . | awk '!/_lang|config/'
./libraries/MY_Encrypt.php: return call_user_func_array(array($this->instance, "encrypt"), func_get_args());
./libraries/Streams/drivers/Streams_cp.php: // As an added measure of obsurity, we are going to encrypt the
./libraries/Streams/drivers/Streams_cp.php: $CI->load->library('encrypt');
./libraries/Streams/drivers/Streams_cp.php: $CI->template->append_metadata('<script type="text/javascript" language="javascript">var stream_id='.$stream->id.'; var stream_offset='.$offset.'; var streams_module="'.$CI->encrypt->encode($CI->module_details['slug']).'";
./modules/comments/libraries/Comments.php: return ci()->encrypt->encode(serialize(array(
./modules/comments/controllers/comments.php: $entry = unserialize($this->encrypt->decode($this->input->post('entry')));
./modules/files/libraries/Files.php: // if we want to replace a file, the file name should already be encrypted, the option was true then
./modules/streams_core/field_types/encrypt/field.encrypt.php:class Field_encrypt
./modules/streams_core/field_types/encrypt/field.encrypt.php: public $field_type_slug = 'encrypt';
./modules/streams_core/field_types/encrypt/field.encrypt.php: $this->CI->load->library('encrypt');
./modules/streams_core/field_types/encrypt/field.encrypt.php: return $this->CI->encrypt->encode($input);
./modules/streams_core/field_types/encrypt/field.encrypt.php: $this->CI->load->library('encrypt');
./modules/streams_core/field_types/encrypt/field.encrypt.php: $out = $this->CI->encrypt->decode($input);
./modules/streams_core/field_types/encrypt/field.encrypt.php: $this->CI->load->library('encrypt');
./modules/streams_core/field_types/encrypt/field.encrypt.php: $options['value'] = ($_POST) ? $params['value'] : $this->CI->encrypt->decode($params['value']);
./modules/streams_core/controllers/ajax.php: $this->load->library('encrypt');
./modules/streams_core/controllers/ajax.php: $module = $this->encrypt->decode($module);
。
答案 2 :(得分:0)
我建议为此任务使用不同的模型。
继续您的示例,您可以拥有蛋白质,每种蛋白质都属于一个类别(根据您对问题的评论中的要求):
class ProteinCategory(models.Model):
name = models.CharField(max_length=20)
class Protein(models.Model):
name = models.CharField(max_length=20)
category = models.ForeignKey(to=ProteinCategory)
然后,您可以为每个三明治分配一个蛋白质(根据您的要求):
class Sandwich(models.Model):
name = models.CharField(max_length=20)
protein = models.ForeignKey(to=Protein)
这似乎可以解决您的问题吗?