还有一个:不能分配“u'3”:“City.state”必须是“状态”实例

时间:2017-07-05 03:55:42

标签: python django

我正在尝试自定义管理员以使用ajax和自定义表单链接2个选择框,一切正常,直到我尝试保存数据并且我收到此错误。 我的模型看起来像这样:

class State(TimeStampModel):
name = models.CharField(
    max_length=200,
    verbose_name=u'State',
    blank=False,
    null=False,
)
code = models.CharField(
    max_length=20,
    verbose_name=u'State Code',
    blank=True,
)
coat_of_arms = models.ImageField(
    upload_to=file_rename('coat_of_arms'),
    verbose_name='Coat of Arms',
    null=True,
    blank=True,
)
country = models.ForeignKey(
    'Country',
    blank=False,
    null=False,
)

def __unicode__(self):
    return self.name

class Meta:
    verbose_name = u'State'
    verbose_name_plural = u'States'


class City(TimeStampModel):
   name = models.CharField(
        max_length=200,
        verbose_name=u'City',
        blank=False,
        null=False,
    )
    code = models.CharField(
        max_length=20,
        verbose_name=u'City Code',
        blank=True,
    )
    coat_of_arms = models.ImageField(
        upload_to=file_rename('coat_of_arms'),
        verbose_name='Coat of Arms',
        null=True,
        blank=True,
    )
    state = models.ForeignKey(
        'State',
        blank=False,
        null=False,
    )

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = u'City'
        verbose_name_plural = u'Cities'

forms.py:

class CityAdminForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
    super(CityAdminForm, self).__init__(*args, **kwargs)
    country_list = self.get_country_choices()
    state_list = self.get_state_choices()

    instance = getattr(self, 'instance', None)

    if instance.pk:
        self.fields['state'].widget.choices = state_list

    self.fields['country'].widget.choices = country_list

state = forms.CharField(label=u'State', required=True,
                        widget=forms.Select)
country = forms.CharField(label=u'Country', required=True,
                          widget=forms.Select)

@staticmethod
def get_country_choices():
    country_list = Country.objects.all()
    choices_list = [(country.id, country.name) for country in country_list]
    choices_list.insert(0, ("", "----------"))

    return choices_list

@staticmethod
def get_state_choices(country_id=None):
    if country_id:
        state_list = State.objects.filter(country_id=country_id)
    else:
        state_list = State.objects.all()
    choices_list = [(state.id, state.name) for state in state_list]
    choices_list.insert(0, ("", "----------"))

    return choices_list

class Meta:
    model = City
    exclude = ['created_at', 'updated_at']

class Media:
    js = ('countries/js/CustomComponents.js', 'countries/js/jquery-ui.js')

我在admin.py中注册了该表单:

class CityAdminForm(admin.ModelAdmin):
fields = ['name', 'code', 'coat_of_arms', 'country', 'state']
form = CityAdminForm

正如我所说的一切正常,直到我尝试保存数据,也许我在表格中遗漏了一些东西。

提前致谢

1 个答案:

答案 0 :(得分:0)

我可以解决问题,让admin创建状态选择框,我在初始化器中删除状态,状态变量的定义 和staticmethod get_state_choices,而everythng现在正在工作,谢谢。