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
JavaScript
x
3
1
class Employer(models.Model):
2
academic_degree = models.CharField(max_length=100, null=True, blank=True)
3
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?
Advertisement
Answer
Two ways to do this:
- Use a
ManyToManyField
and store the academic degrees in another table. E.g:
JavaScript
1
6
1
class AcademicDegree(models.Model):
2
name = models.CharField( )
3
4
class Employer(models.Model):
5
academic_degree = models.ManyToManyField(AcademicDegree)
6
- Use a json or array field (ArrayField only works if you are using Postgres database). This is an example for
JSONField()
:
JavaScript
1
2
1
academic_degree = models.JSONField()
2
You would need to manage this field to treat it as a list.