0

I'm trying to build a ticket system to cover multiple forms for multiple departments. Each ticket form has standard fields for title, date, description. The ticket may need additional fields depending on which department the form is for, i.e marketing might need a where did you hear about us box but accounts will not.

So I have a table that maps field definitions to forms i.e (1) - Where did you hear TextArea ----> (45) Marketing Form

My hope is that based on the form they are selecting, I can show the additional fields relating to the selected form. It didn't generate this by itself when using ModelForm's so I think I'm going to have to do this myself on top.

My first idea is to get all form field objects relating to the particular form (filter(formid=marketing_form)), create fields for them then override the save function to save these form field values before saving the whole form.

I'm new to Django so not sure if there's a better way?

#list of all possible fields for mapping  
class Field(models.Model):
    field_id = models.IntegerField(primary_key=True)
    field_cat = models.ManyToManyField(Category)
    field_label = models.CharField(max_length=50)
    field_type = models.IntegerField(choices=FIELD_TYPE_CHOICES)
    field_enabled = models.BooleanField(default=1)

    def __unicode__(self):
        return self.field_label

#list of all fields additional fields on a ticket 
class TicketField(models.Model):
    tf_id = models.IntegerField(primary_key=True)
    tf_ticket = models.ForeignKey(Ticket)
    tf_field = models.ForeignKey(Field)
    tf_0 = models.CharField(max_length=255, blank=True)
    tf_1 = models.TextField(blank=True)
    tf_2 = models.BooleanField(blank=True)
    tf_3 = models.ForeignKey(Employees, blank=True)
user2355278
  • 375
  • 2
  • 5
  • 15
  • Read [this answer](http://stackoverflow.com/a/16353618/497056). It contains everything that you need. I'm amazed by the popularity of this theme, actually. Today I saw [yet another](http://stackoverflow.com/questions/16398200/using-the-django-formset-to-generate-a-dynamic-form-from-multiple-models/16401820#16401820) question on the [topic](http://stackoverflow.com/a/7934577/497056). Maybe I should start marking them as duplicates... – Ivan Kharlamov May 06 '13 at 17:23
  • 1
    Thank you, http://jacobian.org/writing/dynamic-form-generation/ this has put me on the path – user2355278 May 06 '13 at 18:03

0 Answers0