的Django == 1.11.4
这个模型很好用:
//Add 2D content here
AnchorPane globalRoot = new AnchorPane();
globalRoot.getChildren().add(new Label("Hello World"));
Scene scene = new Scene(globalRoot, 1024, 768, true);
SubScene sub = new
SubScene(root3D,1024,768,false,SceneAntialiasing.BALANCED);
sub.setCamera(camera);
globalRoot.getChildren().add(sub);
//Add all 3D content to the root3D node
primaryStage.setScene(scene);
primaryStage.show();
但后来我想添加另一个也有FileField的模型。新模型看起来与此模型非常相似。唯一的区别是外键。
我确实喜欢这个:
class File(CommonUrlMethodsMixin,
ItemIsMainMixin,
models.Model):
item = models.ForeignKey(Item, on_delete=models.PROTECT)
user_file = models.FileField(blank=False,
max_length=255,
upload_to=get_file_path,
verbose_name=_("file"))
def __str__(self):
if self.user_file.name:
str_repr = "{}:{}".format(str(self.id), self.user_file.name)
else:
str_repr = str(self.id)
return str_repr
我进行了迁移。
forms.py
class GeneralFileMixin():
user_file = models.FileField(blank=False,
max_length=255,
upload_to=get_file_path,
verbose_name=_("file"))
def __str__(self):
if self.user_file.name:
str_repr = "{}:{}".format(str(self.id), self.user_file.name)
else:
str_repr = str(self.id)
return str_repr
class File(CommonUrlMethodsMixin,
ItemIsMainMixin,
GeneralFileMixin,
models.Model):
item = models.ForeignKey(Item, on_delete=models.PROTECT)
views.py
class FileForm(ModelForm):
class Meta:
model = File
exclude = []
widgets = {
'item': forms.HiddenInput()
}
但现在FileCreate没有显示文件的输入字段。
嗯,迁移就在这里:
class FileCreate(LoginRequiredMixin,
CreateView):
model = File
form_class = FileForm
好吧,它确实删除了user_file。但为什么没有考虑mixin中的user_file?
嗯,你能帮我一把吗?答案 0 :(得分:0)
您的新mixin应该继承models
并需要添加abstarct=True
class GeneralFileMixin(models.Model):
# ^^^^^^^^^^^^
user_file = models.FileField(blank=False,
max_length=255,
upload_to=get_file_path,
verbose_name=_("file"))
class Meta:
abstract = True
# ^^^^^^^^^^^^^^
def __str__(self):
if self.user_file.name:
str_repr = "{}:{}".format(str(self.id), self.user_file.name)
else:
str_repr = str(self.id)
return str_repr