Hi - I am working on create form to save a lease made up the following
model, view and form.
Whenever I try to save a lease using the lease form, an integrityerror
pops up saying that the lease.id field is null. Does anyone have an
idea why this might be happening?
#Views.py
def register_new_tenant(request, template_name ='tenant/
register_tenant.html'):
"""
This view takes the three incomplete modelforms Lease, Tenant, and
Rental
period and creates a new lease made up of lease details, its rent
periods
(hopefully plural); and finds and assigns the asset that the
lessee will
occupy.
"""
lform = Add_lease_form(request.POST or None)
tform = Add_tenant_form(request.POST or None)
rform = Rents_formset(request.POST or None)
forms = {'tform' : tform, 'rform' : rform, 'lform' : lform }
if lform.is_valid():
Lease = lform.save(commit = False)
rform = Rents_formset(request.POST or None, instance = Lease)
if rform.is_valid():
Lease.save()
Rents_formset.save()
# Now - how to save the tenant??!?
if tform.is_valid():
Tenant = tform.save(commit = False)
Tenant.lease = Lease
Tenant.save()
return redirect('tenants')
return render(request, template_name, forms)
# Model - minus methods & extraneous fields/
class Asset(models.Model):
address = models.CharField(max_length=50)
class Tenant(models.Model):
lease = models.ForeignKey('Lease')
class Rental_period(models.Model):
lease = models.ForeignKey('Lease')
class Lease(models.Model):
asset = models.ForeignKey(Asset)
#forms.py (with proper imports)
class Add_center_form(ModelForm):
class Meta:
model = Center
class Add_asset_form(ModelForm):
class Meta:
model = Asset
class Add_tenant_form(ModelForm):
class Meta:
model = Tenant
exclude = ['lease',]
class Add_rental_period_form(ModelForm):
class Meta:
model = Rental_period
Rents_formset = inlineformset_factory(Lease,
Rental_period,
can_delete = False,
extra = MAX_RENT_PERIODS)
class Add_lease_form(ModelForm):
class Meta:
model = Lease
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
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.