0

我的模型.py:

class RelayAddress(models.Model):
    id = models.AutoField(primary_key=True,default=0)
    sister_relay_relation = models.ManyToManyField('self', through='RelaySisterRelation',symmetrical=False)

    def save(self, *args, **kwargs):
        self.update_time = int(time.time())
        super(RelayAddress,self).save(*args, **kwargs)

class RelaySisterRelation(models.Model):
    id = models.AutoField(primary_key=True,default=0)
    relay = models.ForeignKey(RelayAddress,related_name="relay")
    sister_relay = models.ForeignKey(RelayAddress,related_name="sister_relay")

我的管理员.py

class RelaySisterRelationForm(forms.ModelForm):
    relay=forms.ModelMultipleChoiceField(label=u'relay',widget=forms.CheckboxSelectMultiple(),queryset=RelayAddress.objects.all())
    sister_relay=forms.ModelMultipleChoiceField(label=u'sister_relay',widget=forms.CheckboxSelectMultiple(),queryset=RelayAddress.objects.all())

    def save(self, *args, **kwargs):
        return super(RelaySisterRelationForm, self).save(*args,**kwargs)

我的 view.py 为空,然后我得到一个 ValueError:

Cannot assign "[<RelayAddress: RelayAddress object>]": "RelaySisterRelation.relay" must be a "RelayAddress" instance.

以及如何解决这个问题。

4

1 回答 1

0

RelaySisterRelation.relay是一个 ForeignKeyRelayAddress意味着它只能存储对一个的引用,RelayAddress但您的RelaySisterRelationForm.relay用途ModelMultipleChoiceField是用于多对多关系,因此返回一个(可能为空的)RelayAddress实例列表。

于 2013-09-02T04:48:32.457 回答