我有多种形式都链接回主属性。我想发生的事情是用户创建了一个Property并将其重定向到在边栏上显示了所有其他表单的页面。然后,用户选择他们想要填写的其他表格。我试图弄清楚如何制作它,以便其余表单已经具有创建的属性作为ForeignKey,而不是让它们通过下拉菜单选择它。真的很感激。
forms.py
class Property(forms.ModelForm):
class Meta:
fields = ('name','address','city','state','zipcode')
model = models.Property
class TenantForm(forms.ModelForm):
class Meta:
fields = ('tenant_name')
model = models.Tenant
def create_view(request, **kwargs):
if request.method == "POST":
aaa = resolve(url).kwargs
form = TenantForm(request.POST)
if form.is_valid():
tenant = form.save(commit=False)
tenant.property_name = aaa
tenant.save()
return reverse('dashboard:tenant', kwargs={'pk':self.pk})
models.py
class Property(models.Model):
name = models.CharField( max_length=255, unique=True)
address= models.CharField(max_length=255,blank=True)
city = models.CharField(max_length=255)
state = models.CharField(max_length=255)
zipcode = models.CharField(max_length=255)
slug = models.SlugField(allow_unicode=True,unique=True, default=uuid.uuid1)
def __unicode__(self):
return "%s %s" % (self.name, self.city)
def __str__(self):
return self.name
def save(self,*args,**kwargs):
self.slug=slugify(self.name)
super().save(*args,**kwargs)
def get_absolute_url(self):
return reverse('dashboard:property_added', kwargs={'slug':self.slug})
class Tenant(models.Model):
property_name= models.ForeignKey(
Property,
on_delete=models.CASCADE,
)
tenant_name = models.CharField(max_length=255, unique=True, default='')
urls.py
path('property/',views.CreateProperty.as_view(),name='property'),
path('property/<slug>/',views.ModifyProperty.as_view(),name='property_added'),
path('tenant/',views.CreateTenant.as_view(),name='tenant'),