Python 还有一个:不能分配;u';3'&引用;:&引用;“城市.国家”;必须是一个";“国家”;实例

Python 还有一个:不能分配;u';3'&引用;:&引用;“城市.国家”;必须是一个";“国家”;实例,python,django,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,

我正在尝试使用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
正如我所说的,在我尝试保存数据之前,一切都正常工作,也许我在表单中遗漏了一些东西


提前感谢

我可以解决这个问题,离开管理员创建状态选择框,我删除了状态初始化器中状态变量的定义 staticmethod获取状态选项,现在一切正常,谢谢