Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

default=CurrentUserDefault and read_only=True in DRF 3.4.0 broken if unique_together is being used in a Model for a ModelSerializer #4294

Closed
Labels
Milestone
@rubendura

Description

@rubendura

Checklist

  • I have verified that that issue exists against themaster branch of Django REST framework.
  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • This is not a usage question. (Those should be directed to thediscussion group instead.)
  • This cannot be dealt with as a third party library. (We prefer new functionality to bein the form of third party libraries where possible.)
  • I have reduced the issue to the simplest possible case.
  • I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

Steps to reproduce

importdjangodjango.setup()fromdjango.contrib.authimportget_user_modelfromdjango.dbimportmodelsfromdjango.confimportsettingsfromrest_frameworkimportserializers,testclassItem(models.Model):user=models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)item_number=models.IntegerField()classMeta:unique_together= (('user','item_number'),)classItemSerializer(serializers.ModelSerializer):user=serializers.PrimaryKeyRelatedField(read_only=True,default=serializers.CurrentUserDefault())classMeta:model=Itemfields= ('id','item_number','user')User=get_user_model()user=User.objects.create_user(username='asdf')request=test.APIRequestFactory().post('/')request.user=useritem=Item.objects.create(user=user,item_number=3)data= {'item_number':item.item_number}context= {'request':request}serializer=ItemSerializer(data=data,context=context)assertnotserializer.is_valid(),'Should be False!'

Expected behavior

The serializer should not be valid. This is the case using djangorestframework == 3.3.0

Actual behavior

The serializer is valid. This is the case using djangorestframework == 3.4.0

I've tracked the root of the issue and it seems to be pull request#4192, where a new condition is added toget_unique_together_validators(): "and not field.read_only"

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp