我有一个像bellow的json数组:
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99,
"likes": 1
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10}
Run Code Online (Sandbox Code Playgroud)
我试图找到没有 …
我有一个应用程序Question,我在其中定义了两个模型Question并Alternative在 models.py 中命名如下:
class Question(models.Model):
question = models.CharField(max_length=255, blank=False, null=False)
chapter = models.ForeignKey(Chapter, on_delete=models.CASCADE)
rating = models.IntegerField(default=1)
class Alternative(models.Model):
alternative = models.CharField(max_length=255, blank=False, null=False)
question = models.ForeignKey(Question, on_delete=models.CASCADE)
Run Code Online (Sandbox Code Playgroud)
我制作了一个自定义表单AlternativeForm,我在其中创建了一个额外的字段,我想将其显示在我的Alternative forms以及Question管理视图中,其中替代字段将出现在内联视图中但额外的字段值不会保存在数据库中(因为我想要使用该字段的值进行一些手动操作)。我forms.py的如下:
class AlternativeForm(forms.ModelForm):
extra_field = forms.BooleanField(required=False)
def save(self, commit=True):
extra_field = self.cleaned_data.get('extra_field', None)
# will do something with extra_field here...
return super(AlternativeForm, self).save(commit=commit)
class Meta:
model = Alternative
fields = '__all__'
Run Code Online (Sandbox Code Playgroud)
在我中,admin.py我在它们之间建立了内联关系,如下所示:
class …Run Code Online (Sandbox Code Playgroud)