Hello all, I'd like some advice on how to approach the design of a calendar based form(set).
This is an example of one of several similar forms I'm working on: ------------------------ Blood glucose readings for Oct 2008 Reading for 11 Oct: |___| Reading for 10 Oct: |___| Reading for 09 Oct: |___| Reading for 08 Oct: |___| Reading for 07 Oct: |___| Reading for 06 Oct: |___| Reading for 05 Oct: |___| Reading for 04 Oct: |5.5| Reading for 03 Oct: |___| Reading for 02 Oct: |4.5| Reading for 01 Oct: |___| ------------------------ Notes: 1. Dates go no further back than users join date 2. Dates go no further forward than today 3. Show entries for user 4. Only save fields that are not empty The point I have got to so far (after trying a variety of methods) is... ------------------------ # models.py class BloodGlucose(models.Model): user = models.ForeignKey(User) date = models.DateField(editable=False) glucose = models.DecimalField(blank=True, max_digits=4, decimal_places=1, verbose_name="Blood glucose") objects = BloodGlucoseManager() class Meta: db_table = 'blood_glucose' ------------------------ # forms.py class BloodGlucoseForm(forms.ModelForm): date = forms.DateField(widget=forms.HiddenInput()) class Meta: model = BloodGlucose fields = ('glucose') exclude = ('user') BloodGlucoseFormSet = modelformset_factory(BloodGlucose, form=BloodGlucoseForm, exclude = ('user')) ------------------------ # views.py if request.method == 'POST': formset = BloodGlucoseFormSet(request.POST) if formset.is_valid(): formset.save() return HttpResponseRedirect('/reading/' + slug + '/') # Redirect after POST else: formset = BloodGlucoseFormSet (queryset=BloodGlucose.objects.form_data(request.user, request_date)) ------------------------ # managers.py class BloodGlucoseManager(models.Manager): def form_data(self, request_user, request_date, data=None): readings = self.readings(request_user.id, request_date) readings = self.model.objects.filter(user=request_user.id) request_month = calendar.monthrange(int(request_date.year),int(request_date.month)) """ Reduce the month down to no further forward than today and no further back than the users join date """ new_month = [] for day in range(1, request_month[1] + 1): current_day = datetime.date(int(request_date.year),int(request_date.month), day) if (current_day >= request_user.date_joined.date() and current_day <= datetime.datetime.today().date()): new_month.append(current_day) new_month.reverse() forms = [] for day in new_month: for reading in readings: if day == reading.date: form = reading break else: form = self.model(date=day, user=request_user) forms.append(form) return forms ------------------------ The main problem with the above solution is that I'm creating a (fake) model object for each day of the month and swapping it with an existing object if the user has a reading row in the database for that date. It seems that on form submission this confuses the save() method. Being a Python/Django newbie, I'd really just like to know if this approach is missing a few tweaks/missing something crucial/missing the point entirely/etc and if there are any tricks/features/alternative approaches I should consider? With thanks, Nick p.s. Please feel free to mock my code as much as is needed! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---