1

I'm struggling to figure out how best to approach this.

I have two models that need to be represented on one page within a form wizard:

class BookingItem(models.Model):
    assignedChildren = models.ManyToManyField('PlatformUserChildren', related_name = "childIDs", null=True)
    quantity = models.PositiveSmallIntegerField(max_length=2,blank=True, null=True)

class PlatformUserChildren(models.Model):
    child_firstname = models.CharField('Childs first name', max_length=30,blank=True, null=True)

The relationship between the models when presenting them on the page is NOT one-to-one. It is governed by quantity attribute and therefore there may be more BookingItems than PlatformUserChildren objects presented on the page (e.g. 4 BookingItems and 2 PlatformUserChildren objects). I loop through each object multiple times based on quantity. I also need to bind to a queryset of the PlatformChildUser model based on the current logged in user.

My question: how do I best present these two models on the first page of my form wizard?

  • I have looked at inline_formsets, but they rely on foreign key relationships only.
  • I have tried a modelformset_factory for one model, and an additional identifier for the other model with some backend reconciliation later, but I'm stuck on how to get a queryset based on user in there
  • i have attempted the get_form_instance method but I'm not 100% sure if it supports querysets
  • finally, I have attempted overloading init however most of the examples are not based on form wizard, and supply external arguments.

My current (rather vanilla) code is below:

forms.py

class checkout_PlatformUserChildren(forms.ModelForm):
    #activity_id = forms.IntegerField()
    class Meta:
        model = PlatformUserChildren
        fields = ('child_age','child_firstname')

    class Meta:
        model = PlatformUserChildren
        fields = ('child_age','child_firstname')
        widgets = {
            'child_firstname': SelectMultiple(attrs={'class': 'form-control',}),
            'child_age' : TextInput(attrs={'class': 'form-control',}),
        }

checkout_PlatformUserChildrenFormSet = modelformset_factory(
        PlatformUserChildren, 
        form = checkout_PlatformUserChildren, 
        fields=('child_firstname', 'child_age'),
        extra=1, max_num=5, can_delete=True)

views.py (done method not shown) note: getUser is an external function that is currently working

checkoutForms = [
    ("assign_child", checkout_PlatformUserChildrenFormSet),
    ("address_information", addressInfo),
        ]

checkoutTemplates = {
                "assign_child": "checkout/assign_child.html",
                "address_information": "checkout/address_information.html",
                }

class checkout(SessionWizardView):
    def get_form_instance(self, step):
        currentUser = getUser(self.request.user.id)
        if step == 'assign_child':
            self.instance = currentUser
        return self.instance
AndrewO
  • 1,105
  • 4
  • 16
  • 33

0 Answers0