models.py
class Category(models.Model):
...
class Article(models.Model):
category = models.ManyToManyField(Category, related_name='articles')
...
class Photography(models.Model):
article = models.ForeignKey(Article, related_name='photos')
image = models.ImageField(upload_to = 'photos')
forms.py
from foo.models import Category, Article, Photography
from django import forms
class EditArticleForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(EditArticleForm, self).__init__(*args,**kwargs)
class Meta:
model = Article
fields = ['category', 'second_field', 'another_one']
widgets = {'category' : forms.ModelMultipleChoiceField(label=''),}
我需要的是用属于编辑表格中文章的第一张照片替换类别标签。当我删除widgets
行时,表单按预期工作但我仍然试图先获取空标签,但出现此错误__init__() takes at least 2 arguments (2 given)
谢谢。