Hey everyone,
I’m working on my first real application and I’m having trouble grasping the concept of saving multiple objects and tying them together. I’m making a real estate app where users can apply to rent a house or apartment. I want a form to allow 1 to 4 people to apply for a single property. I either want 1 page where the user can select the number of applicants and the form generates x number of person forms for the users to fill out, or I want to add 2 buttons to save and complete or save and continue to generate the form again. Either option will work for me I’m just not sure how I will tie them all together in my application table.
models
class Properties(models.Model):
property_num = models.IntegerField()
property_road = models.CharField(max_length=200)
rent = models.IntegerField(default=1000)
city = models.CharField(max_length=200)
state = models.CharField(choices=STATE_CHOICE, max_length=25)
zip = models.IntegerField()
bedrooms = models.IntegerField()
bathrooms = models.FloatField(max_length=3)
sq_foot = models.IntegerField()
description = models.TextField(default=bedrooms)
is_active = models.BooleanField(default=True)
thumbnail = models.ImageField(upload_to=‘static/images’, blank=True)
slug = models.SlugField(blank=True, null=True, unique=True, allow_unicode=True)
class Person(models.Model):
address = models.ForeignKey(Properties, on_delete=models.CASCADE)
requested_move_in_date = models.DateField(default=datetime.now)
first_name = models.CharField(max_length=200)
middle_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
dob = models.CharField(max_length=200)
driver_license = models.IntegerField()
driver_license_state = models.CharField(choices=STATE_CHOICE, max_length=25)
class Application(models.Model):
applicant1 = models.ForeignKey(Person, related_name=‘applicant’, on_delete=models.CASCADE)
applicant2 = models.ForeignKey(Person, related_name=‘co_applicant1’, blank=True, null=True, on_delete=models.CASCADE)
applicant3 = models.ForeignKey(Person, related_name=‘co_applicant2’, blank=True, null=True, on_delete=models.CASCADE)
applicant4 = models.ForeignKey(Person, related_name=‘co_applicant3’, blank=True, null=True, on_delete=models.CASCADE)
property = models.ForeignKey(Properties, on_delete=models.CASCADE)
application_date = models.DateTimeField(default=datetime.now)
Form
class ApplicationForm(forms.ModelForm):
class Meta:
model = Person
fields = ‘all’
# exclude = [‘application_date’]
widgets = {
‘job_start_date’: DateInput(),
‘current_address_end_date’: DateInput(),
‘current_address_start_date’: DateInput(),
‘previous_address_end_date’: DateInput(),
‘previous_address_start_date’: DateInput(),
‘application_date’: forms.HiddenInput(),
View
class ApplicationView(CreateView):
form_class = ApplicationForm
model = Person
template_name = ‘base/application.html’
redirect_field_name = ‘Thankyou’