Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New device auto set active=False #53

Closed
peeomid opened this issue Mar 1, 2018 · 4 comments
Closed

New device auto set active=False #53

peeomid opened this issue Mar 1, 2018 · 4 comments

Comments

@peeomid
Copy link

peeomid commented Mar 1, 2018

Hi,

I'm getting a weird issue, can't seem to find out the reason why, as the default value in device model of active field is set to True.
However when I try to create a new device (without explicitly pass active), active is automatically set to False.

Seems like extra_kwargs = {"active": {"default": True}} doesn't work as expected. Only when active is added to read_only_fields then it works.

Anyone get the same issue?
Thanks

@peeomid
Copy link
Author

peeomid commented Mar 1, 2018

Just a side note, rest_framework.py, under DeviceViewSetMixin, there's this:

if (SETTINGS["ONE_DEVICE_PER_USER"] and
                    self.request.data.get('active', True)):
                FCMDevice.objects.filter(user=self.request.user).update(
                    active=False)

In case active is not pass, then that `self.request.data.get('active') will give back None, which will make the condition False right?

@xtrinch
Copy link
Owner

xtrinch commented Apr 4, 2018

Why would you pass that as extra kwargs if it's already defaulted to active?

@ijajmulani
Copy link

I'm also facing same issue.

@xtrinch extra kwargs sets active to true using below code.

in DeviceSerializerMixin's Meta class
extra_kwargs = {"active": {"default": True}}

but extra_kwargs gets override when
FCMDeviceSerializer's Meta class inherit DeviceSerializerMixin.Meta

So I think below code might need some changes

# Serializers
class DeviceSerializerMixin(ModelSerializer):
    class Meta:
        fields = (
            "id", "name", "registration_id", "device_id", "active",
            "date_created", "type"
        )
        read_only_fields = ("date_created",)
        extra_kwargs = {"active": {"default": True}}

class FCMDeviceSerializer(ModelSerializer, UniqueRegistrationSerializerMixin):
    class Meta(DeviceSerializerMixin.Meta):
        model = FCMDevice
        extra_kwargs = {"id": {"read_only": False, "required": False}}

So when I removed extra_kwargs from DeviceSerializerMixin.Meta class and added it to
FCMDeviceSerializer.Meta in below way

# Serializers
class DeviceSerializerMixin(ModelSerializer):
    class Meta:
        fields = (
            "id", "name", "registration_id", "device_id", "active",
            "date_created", "type"
        )
        read_only_fields = ("date_created",)

class FCMDeviceSerializer(ModelSerializer, UniqueRegistrationSerializerMixin):
    class Meta(DeviceSerializerMixin.Meta):
        model = FCMDevice
        extra_kwargs = {"id": {"read_only": False, "required": False}, "active": {"default": True}}

Then it works.

If you are agree with this change, I would love to make pull request.

@kz26
Copy link
Contributor

kz26 commented May 27, 2018

I'm also experiencing this issue on the latest version 0.2.19.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants