Tuesday, May 31, 2016

Re: parent: [ 'Invalid pk "0" - object does not exist.' ] and null or blank value not store in mysql



On Tue, May 31, 2016 at 5:21 AM, L K Singh <lksingh.india@gmail.com> wrote:
class AccountType(ModelBase):
parent = models.ForeignKey('self',on_delete = models.SET_NULL, null=True, blank=True, default=None )

class AccountTypeSerializer(serializers.ModelSerializer):
parent = serializers.PrimaryKeyRelatedField(queryset=AccountType.objects.all(), required=False,null=True, blank=True)
class Meta:
model = AccountType
fields = ('id','parent','account_type_name')

how to set null and empty value in parent field
A null value and an empty value are two separate and different things (None != ''). DRF will exclude 'parent' from the serialized data if it is not included in your input data and you have no default= in your serializer definition. 

If I understand you right, you want a parent=None key-value pair in your serializer output, even if one isn't provided by the model you are serializing. In that case, I believe all you need to do is add 'default=None' to your 'parent' field in your serializer.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciWUWkP6L-RKdgUZBMuj9Nv%3DAzzJ-EFz4GNLQQM%2BR3R-eA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment