0

first question.

I'm having a problem with creating a structure of creating an event with several (user specifies how many) activities and each activity may occur more than once. The structure I'd like my form to have is:

\-- Event
  Activity 1
    Time 1
    Time 2
    Time 3
  Activity 2
    Time 1
    Time 2
    Time 3

Where the user can add more activities, and for each activities more times.

models.py

def Event(models.Model):
  # not relevant attrs

def Activity(models.Model):
  event = models.ForeignKey(Event)
  # more irrelevant stuff

def ActivityTime(models.Model):
  activity = models.ForeignKey(Activity)
  start_time = models.TimeField()
  end_time = models.TimeField()
  # more irrelevant stuff

I'm using modelforms to represent Event, Activity, ActivityTime. I want each time to be 'aware' of its corresponding activity for when I add the entries to the database. I'm not sure how to implement this. I haven't had luck with inline formsets but maybe because there's something I’m not understanding.

  • you have the ForeignKey field from `ActivityTime` to `Activity`, so inline formsets are the way to go https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#inline-formsets – Anentropic Jul 31 '14 at 22:47
  • I've been looking at inlineformset_factory, the problem is that every Activity has to be 'aware' of it's ActivityTime. So in my template I want to have a blank ActivityTimes for each Activity and that's where I'm having troubles :/ – Alex Yakubovsky Aug 01 '14 at 00:36

2 Answers2

0

An inline formset is a type of model formset.

To create your formset you will use an inlineformset_factory. If you have a look at the docs here:
https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#limiting-the-number-of-editable-objects

...you'll see that it supports two args max_num and extra which together determine whether and how many extra blank forms will be added to the formset.

If you google for 'django dynamic formset' you will find several packages which provide some javascript to add more 'extra' blank forms to the formset from the frontend side (same as you can do in Django admin).

Anentropic
  • 32,188
  • 12
  • 99
  • 147
  • The inlineformset_factory seems to work if I have a fixed number of forms, but because for my application I may have more/less fields, it was difficult to keep sub-forms aware of their children (if that makes sense?) – Alex Yakubovsky Aug 02 '14 at 17:38
  • the point of an inlineformset is that the forms are always aware of their children... have you read the docs? you don't have to have a fixed number of forms, that what the `extra` parameter is for – Anentropic Aug 02 '14 at 18:05
  • Sorry, you are absolutely correct. My problem is dynamically creating the forms, to which you recommended the google search. You think I may as well work with angular and validate the info once I pass it to serverside? – Alex Yakubovsky Aug 02 '14 at 18:18
  • yes, as long as you follow the naming convention for the form fields then Django will recognise the dynamically added extra forms in the formset – Anentropic Aug 02 '14 at 18:30
  • I meant without using django forms, but rather the same method as shown here: http://stackoverflow.com/questions/16168355/angularjs-server-side-validation-and-client-side-forms – Alex Yakubovsky Aug 02 '14 at 18:44
  • yes but on the server side you still want to post them to a Django formset in your view – Anentropic Aug 02 '14 at 19:02
0

I'm not sure if this is the best way, but I decided to use angularjs for dynamically create the forms, and pass the data through a post call to validate it on the server side. For anyone interested, I followed similarly to this stack question: AngularJS - Server side validation and client side forms

Community
  • 1
  • 1