0
我有兩個模型公司和Campaign。我需要在他們之間建立一種關係。我認爲我的模型很好。使用基於django類的視圖創建兩個模型之間的關係
公司/ model.py
class Company(models.Model):
class Meta:
verbose_name_plural = "companies"
user = models.ForeignKey(settings.AUTH_USER_MODEL)
title = models.CharField(blank=False, max_length=128, default='')
slug = models.SlugField(blank=True, unique=True)
archived = models.BooleanField(default=False)
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
活動/ models.py
class Campaign(models.Model):
class Meta:
verbose_name_plural = "campaigns"
company = models.ForeignKey('companies.Company', on_delete=models.CASCADE,)
title = models.CharField(blank=False, max_length=128, default='')
slug = models.UUIDField(default=uuid.uuid4, blank=True, editable=False)
def __str__(self):
return str(self.title)
活動/ forms.py
class CampaignForm(forms.ModelForm):
class Meta:
model = Campaign
fields = ['title','description','archived']
個活動/ views.py
class CampaignCreateView(SubmitBtnMixin, CreateView):
model = Campaign
company = None
form_class = CampaignForm
submit_btn = "Add Campaign"
template_name = "form.html"
活動/ urls.py
url(r'^campaign/create/$', CampaignCreateView.as_view(), name='campaign-create'),
我的問題是,創建一個新的活動,在哪裏以及如何做我拿起公司PK的時候填充Campaign模型?做這件事最安全和最好的做法是什麼?