1

Hello I have a form page and I only need users to fill in certain fields, with the rest of the fields being pre-filled for them based on the module they pick.

While I can fetch the objects from my database -- i.e. the dropdown list shows Module Object (1), Module Object (2) -- I need only certain fields in these objects, which is why this similar sounding post couldn't answer my question: Populate a django form with data from database in view

Here's my forms.py

class inputClassInformation(forms.Form):
    module = forms.ModelChoiceField(queryset=Module.objects.all())
    duration = forms.CharField(disabled=True, required=False)
    description = forms.CharField()
    assigned_professors = forms.ModelChoiceField(queryset=Class.objects.filter(id='assigned_professors'))

models.py -- not the full models are shown to reduce the post's length

class Module(models.Model):
    subject = models.CharField(max_length=200, default="")

class Class(models.Model):
    module = models.ForeignKey(Module, on_delete=models.CASCADE, default="")
    duration = models.CharField(max_length=200, default="")
    description = models.CharField(max_length=200, default="")
    assigned_professors = models.CharField(max_length=200, default="")

So an expected result would be:
1) The Module field shows the subjects, instead of Module objects in its dropdown list and
2) The duration field is automatically filled in for the user, based on the module they picked. The reason is so that the user only has to manually fill in certain fields, while the rest are automatically generated.

This has had me stuck for a long while, help is appreciated. Thanks!

IceTea
  • 598
  • 1
  • 6
  • 19

1 Answers1

2

So an expected result would be:

1) The Module field shows the subjects, instead of Module objects in its dropdown list and

2) The duration field is automatically filled in for the user.

These are essentially two different questions.

To answer the first: you can override the:

  • __str__ method for your Model class for python 3 and django 1.8) and the
  • __unicode__ method for your Model class for django <1.8 and not python3.

For example, to make subjects appear instead of "XXXX Object" for your class do:

class Module(models.Model):
    subject = models.CharField(max_length=200, default="")

    def __unicode__(self):
        return self.subject

Similarly, change __unicode__ for __str__ as appropriate for your django version.

Now, to answer your second question:

2) The duration field is automatically filled in for the user.

You need to do two things:

  1. Do not display the duration field in your form (unless you want to give the users the chance to sometimes fill it in manually)
  2. Override the save method

An example:

class Class(models.Model):
    module = models.ForeignKey(Module, on_delete=models.CASCADE, default="")
    duration = models.CharField(max_length=200, default="")
    description = models.CharField(max_length=200, default="")
    assigned_professors = models.CharField(max_length=200, default="")

    def save(self, *args, **kwargs):
        self.duration = #The value you'd want to automatically set here#
        super(Model, self).save(*args, **kwargs)
Misho Janev
  • 512
  • 5
  • 13
  • thanks for the first answer! However, for the second one, I don't want to fix a fixed duration for every class; I would like the duration to be fetched from the database based on the module the user picked (and the fields the user fills in will subsequently update the entry in the database). I mentioned this in the beginning of my post, but not at the end, so I've added it there too! – IceTea Apr 20 '19 at 11:04