我创建了一个salesmanager应用程序,它包含两个模型Company和Item,例如
#sales/models.py
class Company(models.Model):
name = models.CharField(max_length=255)
phone = models.CharField(max_length=9)
mobile = models.CharField(max_length=10)
address = models.ForeignKey(Address)
contact_person = models.ForeignKey(ContactPerson)
def __str__(self):
return self.name
class Item(TimeStampedModel):
"""Item model """
name = models.CharField("Item name", max_length=255)
tag = models.CharField("Item tag", max_length=30, unique=True)
category = models.ForeignKey(Category)
company = models.ForeignKey(Company)
price = models.DecimalField(max_digits=15, decimal_places=2)
def __str__(self):
return self.tag + "-" + self.name
我想要的是创建一个表单,使用单个按钮从单个页面更新这些模型。为了实现这一点,我创建了一个基于类的视图,这样
from django.shortcuts import render
from django.views.generic.list import ListView
from django.views.generic.edit import CreateView
class ItemCreateView(CreateView):
"""Create a item """
template_name = 'salesmanager/item_create_form.html'
success_url = '/dashboard'
model = Item
#created_at and updated_at fields are from TimeStamped model
fields = ('name','tag', 'price', 'created_at', 'updated_at')
我想解决的问题是,如何在item_create_from.html
模板中呈现外键字段?
另一个问题是created_at
和updated_at
继承自TimeStampedModel(从django的Twoscoops中学习)在CreateView类中无法识别。
我们不能使用基于类的视图创建多关系表单。如果不是,我应该使用基于功能的视图。