Forms can be complicated in Django. Formsets can make you want to quit Django. I'm at that point.
What are the different use cases and considerations of which one(s) to use?
I'm looking for some better guidance as to when to use each factory, as they seem to depend on when you know what type of form, fields, and whether or not you are creating, editing, or deleting (individual forms entirely or the parent model altogether). I have read many walkthroughs, but am struggling to see the larger picture, especially as I am attempting to move from function based views to Class Based Views.
Below are some pseudo code with assumptions/restrictions to help you help me understand the differences. It may help to provide psuedocode, such as what kind of Form (ModelForm or regular) goes with the Formset, or what should be pop
ped from the form, given this seems to be a trend for creating forms with relations.
Assuming you have some models:
class Dish(models.Model):
name = models.CharField(max_length=50)
class Meal(models.Model):
name = models.CharField(max_length=50)
dishes = models.ManyToManyField(Dish,
# through='OPTIIONALMealDishIntermediaryClassTable',
related_name="meal")
class Reservation(models.Model):
date = models.DateTimeField()
greeting = models.CharField(max_length=255)
meal = models.OneToOneField(Meal, on_delete=models.CASCADE)
class MealPhotos(models.Model):
photo = models.OneToOneField(Photo, on_delete=models.CASCADE, related_name='mealPhoto')
meal = models.ForeignKey(Meal, on_delete=models.CASCADE)
# optional, so a photo can be attached to a dish if the picture is just of the dish
dish = models.ForeignKey(Dish, blank=True, null=True, on_delete=models.CASCADE)
And you want to create a new Meal
, you want to send a Reservation
at the same time:
- which factory would you use?
- does it depend on if the forms are all
ModelForms
? (meaning how would you handle assignming theMeal
itsReservation
) - assuming:
- at this stage you know which
Meal
it is, but you still have to make aReservation
at the same time/same view. - you don't know which
Dish
es you are going to cook, since the reservation will tell you. MealPhotos
won't exist yet since the meal isn't prepared yet.- You want to create the meal and the reservation on the same form/screen
- at this stage you know which
Then later, you want to add some dishes, based on what the Reservation
says:
- which factory would you use?
- does it depend on if the forms are all
ModelForms
? - assuming:
- at this stage you know which
Meal
it is, and you have aReservation
- you are going to assign dishes to the meal based on the
Reservation
, and you have enough information to do so, and can use a ModelForm easily, but not required
- at this stage you know which
Later, the person eating the dish wants to take some photos, and you don't know how many they will take
- which factory would you use?
- does it depend on if the forms are all
ModelForms
? - assuming:
- we will require them to take at least two
- we have access to the
Meal
,Reservation
, andDishes
- a photo could optionally be assigned to a
Dish