简体   繁体   English

将多个值添加到 django 模型的属性

[英]Add multiple values to a django model's attribute

I want to add an attribute to a django model as much as the user wants to.我想根据用户的需要向 django 模型添加一个属性。
For example I want to add a few academic degrees比如我想加几个学历

class Employer(models.Model):
    academic_degree = models.CharField(max_length=100, null=True, blank=True)

with this code We can just add one degree and if a person has more, he or she can't add them.使用此代码我们可以只添加一个学位,如果一个人有更多,他或她不能添加它们。
I need a way to add as much degrees as i want in django forms.我需要一种在 django 表单中添加尽可能多的度数的方法。 Is that possible?那可能吗?

Two ways to do this:有两种方法可以做到这一点:

  1. Use a ManyToManyField and store the academic degrees in another table.使用ManyToManyField并将学位存储在另一个表中。 Eg:例如:
class AcademicDegree(models.Model):
    name = models.CharField(...)

class Employer(models.Model):
    academic_degree = models.ManyToManyField(AcademicDegree)
  1. Use a json or array field (ArrayField only works if you are using Postgres database).使用 json 或数组字段(ArrayField 仅在您使用 Postgres 数据库时才有效)。 This is an example for JSONField() :这是JSONField()的示例:
academic_degree = models.JSONField()

You would need to manage this field to treat it as a list.您需要管理此字段以将其视为列表。

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

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