#Models.py
class Appname(models.Model):
name=models.CharField(max_length=150,blank=False,null=False)
def __str__(self):
return self.name
class placement(models.Model):
user=models.ForeignKey(User,related_name='placeid', null=True, default=None,on_delete=models.CASCADE)
name=models.ForeignKey(Appname,related_name='appname',null=True,default=None,on_delete=models.CASCADE,editable=False)
ad_space=models.CharField(max_length=150,blank=False,null=False)
PID_TYPE = (
('FN','FORMAT_NATIVE'),
('FNB','FORMAT_NATIVE_BANNER'),
('FI','FORMAT_INTERSTITIAL'),
('FB','FORMAT_BANNER'),
('FMR','FORMAT_MEDIUM,RECT'),
('FRV','FORMAT_REWARDED_VIDEO'),
)
format = models.CharField(max_length=3,choices = PID_TYPE,default = 'FN',blank=False, null=False)
pid=models.CharField( max_length=50,default='',blank=False, null=False)
cpm=models.IntegerField(default=0,blank=False, null=False)
ADS_TYPE=(
('FB','FACEBOOK'),
('G','GOOGLE'),
) source=models.CharField(max_length=2,choices=ADS_TYPE,default='FB',blank=False, null=False)
comments=models.TextField(default='',blank=False, null=False)
objects=models.Manager()
def __str__(self):
return self.ad_space
def get_absolute_url(self):
return reverse("dashapp:view")
我现在使用通用视图通过表单保存第一个模型,现在我在第二个模型中使用了ForeignKey。我希望当我填写第二个Form时,name变量自动填充第一个模型值。我能做到吗?
答案 0 :(得分:0)
您可以使用Form的初始参数 https://docs.djangoproject.com/en/2.1/ref/forms/api/#django.forms.Form.initial
name = Appname.objects.get(name='name')
form = SecondForm(initial={'name': name})