在admin内联中使用自动增量值填充字段

时间:2014-11-22 20:01:45

标签: python django django-admin

我有以下代码:

class Question (models.Model):

    question_text = models.CharField(max_length=200)

class Choice (models.Model):

     question = models.ForeignKey(Question)
     number = models.PositiveSmallIntegerField()
     choice_text = models.CharField(max_length=200)
     votes = models.IntegerField(default=0)

from django.contrib import admin
from app.models import Question, Choice

class ChoiceInline(admin.TabularInline):
    model = Choice
    extra = 3

class QuestionAdmin(admin.ModelAdmin):

    fieldsets = [
        (None,               {'fields': ['question_text']}),
    ]
    inlines = [ChoiceInline]

admin.site.register(Question, QuestionAdmin)
admin.site.register(Choice)

我要添加自动增量编号字段。添加问题时我有选项卡,我添加选项。有一个字段。默认情况下它是空的。我想用从1开始的自动数字填写它。怎么做?

1 个答案:

答案 0 :(得分:0)

所以,我想你想要保存Choice s的顺序?

您可以覆盖QuestionAdmin类中的方法save_related,该方法负责保存所有相关对象,只需迭代设置订单属性的对象。