These are chat archives for django/django

18th
Nov 2016
sukianto
@sukianto
Nov 18 2016 03:55
Guys I have a code, that I want to save from serializer to model serializer can someone help me?
Thanks :)
class DeviceSerializer(serializers.ModelSerializer):
    """
    Serializer for Device model.
    """

    class Meta:
        model = models.Device
        fields = ('user', 'reg_id', 'type', 'is_active')


class DeviceRegisterSerializer(serializers.Serializer):
    """
    Serializer for registering device
    """
    reg_id = serializers.CharField(required=True, write_only=True, max_length=152, min_length=152)
    type = serializers.CharField(required=True, write_only=True, max_length=7)

    device = DeviceSerializer()

    def validate_type(self, value):
        if not (value == 'android') | (value == 'ios') | (value == 'website'):
            msg = 'The type of device is not recognized.'
            raise ValidationError(msg)

        return value

    def validate(self, attrs):
        return attrs

    def save(self):
        self.device.save()
rjvcoder
@rjvcoder
Nov 18 2016 04:50
Hi! Anybody used sublime text for django before?
Michael-J-Ward
@Michael-J-Ward
Nov 18 2016 10:25
@sukianto the two main things that ModelSerializer provides is (reasonable) automated field mappings and (reasonable) behavior on save and update. You should not overwrite a serializer's .save directly unless you are trying to do something unique. Instead, you should define a .create(self, validated_data) and a .update(self, instance, validated_data), and the serializer will use the appropriate method based on whether you give it an object instance upon instantiation
lastly, have you gone through the django-rest tutorial? http://www.django-rest-framework.org/tutorial/1-serialization/
sukianto
@sukianto
Nov 18 2016 10:27
Yes, I try the tutorial, but I need the serialization from the post method on view right?
and get serializer for model
Michael-J-Ward
@Michael-J-Ward
Nov 18 2016 10:32
I do not follow.
sukianto
@sukianto
Nov 18 2016 10:32
How if I want to make different input?
So if my post method on views.py just need reg_id and type
cause user and is_active are generated
but ModelSerializer need 4 item
sorry my bad english :)
Michael-J-Ward
@Michael-J-Ward
Nov 18 2016 10:36
class DeviceSerializer(serializers.ModelSerializer):
    """
    Serializer for Device model.
    """

    class Meta:
        model = models.Device
        fields = ('reg_id', 'type')

then in the create view

serialized = DeviceSerializer(**request.data)
if serialized.is_valid():
    serialized.save(user=request.user)

Not sure how is_active should be handled in your case=

sukianto
@sukianto
Nov 18 2016 10:42

Ah, ok so I just add

serialid.save(user=request.user,is_active=True)

if I want to put the is_active ?

Michael-J-Ward
@Michael-J-Ward
Nov 18 2016 10:45
yessr
sukianto
@sukianto
Nov 18 2016 10:45
@Michael-J-Ward but how if I want to
for ex:
post -> reg_id, type required
update -> reg_id, type, user required
last question :)
Michael-J-Ward
@Michael-J-Ward
Nov 18 2016 10:46
you want user to be required on update but not create?
sukianto
@sukianto
Nov 18 2016 10:47
Hmm, I don't get it
what I want is
post -> 2 items required
update -> 3 items required
Michael-J-Ward
@Michael-J-Ward
Nov 18 2016 10:52
A) That is a very unusual use case.
class DeviceSerializer(serializers.ModelSerializer):
    """
    Serializer for Device model.
    """

    class Meta:
        model = models.Device
        fields = ('user',  'reg_id', 'type')

And in create view

payload = request.data
payload['user'] = request.user
serialized = DeviceSerializer(**payload)
if serialized.is_valid():
    serialized.save(is_active=True)

And then if user is included in the payload on update, then just remove the payload['user'] = request.user line

sukianto
@sukianto
Nov 18 2016 11:04

@Michael-J-Ward Hmm, that's 3 items required, the different is input on request.user and the other input on request.post, make senses, ok thank u very much :)

Just to confirm
So it's not possible using serializer.save() on serializer
just can do it on view
is it correct?

And also not possible having 2 serializers with 1 models
ah nvm about the last one, it can do by subclassing and overwrite :)
Simon Frid
@fridiculous
Nov 18 2016 21:12
I'm creating a django extension and i'm looking for some api design feedback. Poll: do you use myclass.objects.get_or_create often?
what do you think of having get_or_create called within obj.save(), or using another shortcut called obj.osave(), obj.gcsave() or something like that
Dima Gîra
@dimmg
Nov 18 2016 21:16
hi, does any of you have any experience with raml?