/ imagetarget上的Django ValueError /无法分配“<simplelazyobject:<user:=”“dip7777 =”“>&gt;”:

时间:2015-11-21 13:14:40

标签: python django django-rest-framework

错误消息

Cannot assign "<SimpleLazyObject: <User: dip7777>>": "ImageTarget.uploaderclient" must be a "UploaderClient" instance.

我正在使用Python 3.4.3和Django 1.8并使用Django Rest Framework 3。 我正在使用DRF可浏览的API进行测试。

我要做的是将当前登录的用户附加到他上传的文件中。

每当我尝试执行POST并上传具有登录用户的文件时,都会引发上述错误。

我创建了几个UploaderClient,用户dip7777就是这样一个绑定到UploaderClient的用户。我已使用该用户登录。

使用当前代码我能够完成的是编辑当前上传的文件,即。使用ImageTargetDetail视图上传新文件代替当前文件。 (我上传了几个文件并使用管理界面将它们绑定到UploaderClients

我想要做的是使用ImageTargetList视图上传新文件,而不是替换先前存在的文件。 但错误显示,我不明白如何将当前(已登录)uploaderclient实例分配给ImageTarget的{​​{1}}

我有两个模型1)Upl​​oaderClient

uploaderclient

和2)ImageTarget

class UploaderClient(models.Model):
    user = models.OneToOneField(User)
    company_name = models.CharField(_('company name'), max_length=100)
    class Meta:
        verbose_name = _('uploaderclient')
        verbose_name_plural = _('uploaderclients')
    def __str__(self):
        return 'UploaderClient: {}'.format(self.user.username)

在两个不同的应用class ImageTarget(models.Model): uploaderclient = models.ForeignKey('authenticateclients.UploaderClient', related_name='imagetargets') targetName = models.CharField(max_length=50, unique=True) imageWidth = models.IntegerField() targetFile = models.FileField(upload_to=get_upload_imagetargetfile_name) authenticateclients

我的序列化器是:

clientupload

class UploaderClientSerializer(serializers.HyperlinkedModelSerializer):
    username = serializers.CharField(source='user.username')
    email = serializers.CharField(source='user.email', required=False, allow_null=True)
    password = serializers.CharField(source='user.password', write_only=True, required=False)
    date_joined = serializers.DateTimeField(source='user.date_joined', read_only=True)
    last_login = serializers.DateTimeField(source='user.last_login', read_only=True)
    company_name = serializers.CharField()
    imagetargets = serializers.HyperlinkedRelatedField(many=True, view_name='imagetargetsdetail', read_only=True)
    url = serializers.HyperlinkedIdentityField(view_name='uploaderclientsdetail')
    class Meta:
        model = UploaderClient
        fields = ('url', 'email', 'username', 'company_name', 'date_joined', 'last_login', 'password','imagetargets',)
        read_only_fields = ('user.date_joined', 'user.last_login',)

我的观点是

class ImageTargetSerializer(serializers.HyperlinkedModelSerializer):
    uploaderclient = UploaderClientSerializer(read_only=True,required=False)
    targetFile = serializers.FileField(label='TargetFile')
    url = serializers.HyperlinkedIdentityField(view_name='imagetargetsdetail')
    class Meta:
        model = ImageTarget
        fields = ('url','uploaderclient','targetName','imageWidth','targetFile',)
    def get_validation_exclusions(self):
        exclusions = super(ImageTargetSerializer, self).get_validation_exclusions()
        return exclusions + ['uploaderclient']

class UploaderClientList(generics.ListAPIView):
    queryset = UploaderClient.objects.all()
    serializer_class = UploaderClientSerializer
class UploaderClientDetail(generics.RetrieveAPIView):
    queryset = UploaderClient.objects.all()
    serializer_class = UploaderClientSerializer

1 个答案:

答案 0 :(得分:0)

在ImageTargetList.perform_create中:

 $(".ideatorInfo").hide();
      $('.ideator').on('click', function() {
        $(this).parent().find(".ideatorInfo").each(function(){
           $(this).show();
           $(this).dialog({
               autoOpen: false
           }).dialog('open');
        }
      });

但是uploaderclient不应该是用户。