I have a record keeping system that consists of two models. One model creates a holding page and the other creates individual entries…
class RecordEntry(models.Model):
client = models.CharField(max_length=50, choices=CLIENT_CHOICES)
job_number = models.CharField(max_length=30, unique=False, blank=False, null=False)
cell_number = models.CharField(max_length=4, unique=False, blank=True, null=True)
entry_title = models.CharField(max_length=64, unique=False, blank=True, null=True)
date = models.DateField(("Date"), default=datetime.date.today)
attachment = models.FileField(upload_to=content_file_name, blank=True, null=True)
...
class RecordPage(models.Model):
client = models.CharField(max_length=50, choices=CLIENT_CHOICES)
job_number = models.CharField(max_length=30, unique=True, blank=False, null=False)
job_name = models.CharField(max_length=64, unique=False, blank=False, null=False)
create_date = models.DateField(("Date"), default=datetime.date.today)
contact = models.ForeignKey(UserProfile)
entry = models.ForeignKey(RecordEntry, blank=True, null=True)
...
…where the page might be #211211
and the individual entries might be #211211-01, #211211-02, #211211-03
For simplicity, I’m using the forms rendered in admin to create the pages. It’s convenient because the admin gives me the “plus” icon that allows me to create and add an entry on the fly. My problem is, I’d like to be able to add as many “entries” to the page as I need. I mean, to get multiple “entry” fields on a form I could do something like:
class RecordPage(models.Model):
client = models.CharField(max_length=50, choices=CLIENT_CHOICES)
job_number = models.CharField(max_length=30, unique=True, blank=False, null=False)
job_name = models.CharField(max_length=64, unique=False, blank=False, null=False)
create_date = models.DateField(("Date"), default=datetime.date.today)
contact = models.ForeignKey(UserProfile)
entry = models.ForeignKey(RecordEntry, blank=True, null=True)
entry2 = models.ForeignKey(RecordEntry, related_name='+', blank=True, null=True)
entry3 = models.ForeignKey(RecordEntry, related_name='+', blank=True, null=True)
entry4 = models.ForeignKey(RecordEntry, related_name='+', blank=True, null=True)
entry5 = models.ForeignKey(RecordEntry, related_name='+', blank=True, null=True)
entry6 = models.ForeignKey(RecordEntry, related_name='+', blank=True, null=True)
entry7 = models.ForeignKey(RecordEntry, related_name='+', blank=True, null=True)
and so on…
but there has to be a better way, right?
why don’t use ManyToManyField here
And Django admin will automatically gives you the ability to add new rows.