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

1,577 views
Skip to first unread message

L K Singh

unread,
May 31, 2016, 8:27:20 AM5/31/16
to Django users
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

James Schneider

unread,
May 31, 2016, 12:26:44 PM5/31/16
to django...@googlegroups.com
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.


Reply all
Reply to author
Forward
0 new messages