简体   繁体   中英

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.
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. Is that possible?

Two ways to do this:

  1. Use a ManyToManyField and store the academic degrees in another table. 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). This is an example for JSONField() :
academic_degree = models.JSONField()

You would need to manage this field to treat it as a list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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