Someone please help me. I want to add repeatable group fields in the models in my Django project.
class Mygroupfields(models.Model):
invioceNo = models.CharField(max_length=10)
invoiceDate = models.DateField()
price = models.DecimalField(max_digits=7, decimal_places=2)
I want to repeat these three fields as required. It may be two/three/four/ n times.
Please provide a more detailed description of what you’re trying to achieve, and what Model(s) you have so far, and what you have tried.
class Mygroupfields(models.Model):
invioceNo = models.CharField(max_length=10)
invoiceDate = models.DateField()
price = models.DecimalField(max_digits=7, decimal_places=2)
The pattern to be used here is a base model - something that every instance has in common.
For example, the instance might be an Invoice. You would then have a different model - your “MyGroupFields”, where all the instances of that group are related to that invoice.
e.g.
class Invoice(...):
invoiceNo = ...
invoiceDate = ...
class MyGroupFields(...):
Invoice = ForeignKey(Invoice, ...)
price = ...
(other fields as required)
Note, you are not working in PHP here. The models you are creating are the visible layer over top of a relational database. You want to create your models such that they are consistent with a well-designed database.