What determines what fields can be updated in DjangoRestFramework











up vote
0
down vote

favorite












I currently have a serializer with the following fields



class Meta:
model = Asset
fields = ('id', 'uuid', 'asset_category', 'asset_sub_category',
'make_label',
'asset_code', 'serial_number', 'model_number',
'checkin_status', 'created_at',
'last_modified', 'current_status', 'asset_type',
'allocation_history', 'specs', 'purchase_date',
'notes', 'assigned_to', 'asset_location'
)


Serializer



class AssetSerializer(serializers.ModelSerializer):
checkin_status = serializers.SerializerMethodField()
allocation_history = serializers.SerializerMethodField()
assigned_to = UserSerializer(read_only=True)
asset_category = serializers.SerializerMethodField()
asset_sub_category = serializers.SerializerMethodField()
make_label = serializers.SerializerMethodField()
asset_type = serializers.SerializerMethodField()
model_number = serializers.SlugRelatedField(
queryset=AssetModelNumber.objects.all(),
slug_field="model_number"
)

class Meta:
model = Asset
fields = ('id', 'uuid', 'asset_category', 'asset_sub_category',
'make_label',
'asset_code', 'serial_number', 'model_number',
'checkin_status', 'created_at',
'last_modified', 'current_status', 'asset_type',
'allocation_history', 'specs', 'purchase_date',
'notes', 'assigned_to', 'asset_location'
)
depth = 1
read_only_fields = ("uuid",)


View



class ManageAssetViewSet(ModelViewSet):
serializer_class = AssetSerializer
queryset = Asset.objects.all()
# permission_classes = [IsAuthenticated, IsAdminUser]
# authentication_classes = (FirebaseTokenAuthentication,)
http_method_names = ['get', 'post', 'put', 'delete']
filter_backends = (filters.DjangoFilterBackend,)
filterset_class = AssetFilter

def get_object(self):
queryset = Asset.objects.all()
obj = get_object_or_404(queryset, uuid=self.kwargs['pk'])
return obj


Model
Asset Model. Some fields have been ommited



class Asset(models.Model):
"""Stores all assets"""
uuid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
asset_code = models.CharField(
unique=True, null=True, blank=True, max_length=50)
serial_number = models.CharField(
unique=True, null=True, blank=True, max_length=50)
created_at = models.DateTimeField(auto_now_add=True, editable=False)
asset_location = models.ForeignKey('AndelaCentre', blank=True, editable=True, null=True,
on_delete=models.PROTECT)
purchase_date = models.DateField(
validators=[validate_date],
null=True, blank=True)
last_modified = models.DateTimeField(auto_now=True, editable=False)
assigned_to = models.ForeignKey('AssetAssignee',
blank=True,
editable=False,
null=True,
on_delete=models.PROTECT)
model_number = models.ForeignKey(AssetModelNumber,
null=True,
on_delete=models.PROTECT)
current_status = models.CharField(editable=False, max_length=50)
notes = models.TextField(editable=False, default=" ", )


However, on the browsable Api, only 4 fields are showing on the UPDATE/PUT form as shown in the diagram below
enter image description here



What could be the reason some of the other fields are not appearing here. What determines which fields are updatable??










share|improve this question




















  • 2




    Share model view and serializer
    – a_k_v
    Nov 8 at 9:59










  • Just added them
    – Esir Kings
    Nov 8 at 10:03










  • add model details?
    – a_k_v
    Nov 8 at 10:07










  • Just added them
    – Esir Kings
    Nov 8 at 10:11















up vote
0
down vote

favorite












I currently have a serializer with the following fields



class Meta:
model = Asset
fields = ('id', 'uuid', 'asset_category', 'asset_sub_category',
'make_label',
'asset_code', 'serial_number', 'model_number',
'checkin_status', 'created_at',
'last_modified', 'current_status', 'asset_type',
'allocation_history', 'specs', 'purchase_date',
'notes', 'assigned_to', 'asset_location'
)


Serializer



class AssetSerializer(serializers.ModelSerializer):
checkin_status = serializers.SerializerMethodField()
allocation_history = serializers.SerializerMethodField()
assigned_to = UserSerializer(read_only=True)
asset_category = serializers.SerializerMethodField()
asset_sub_category = serializers.SerializerMethodField()
make_label = serializers.SerializerMethodField()
asset_type = serializers.SerializerMethodField()
model_number = serializers.SlugRelatedField(
queryset=AssetModelNumber.objects.all(),
slug_field="model_number"
)

class Meta:
model = Asset
fields = ('id', 'uuid', 'asset_category', 'asset_sub_category',
'make_label',
'asset_code', 'serial_number', 'model_number',
'checkin_status', 'created_at',
'last_modified', 'current_status', 'asset_type',
'allocation_history', 'specs', 'purchase_date',
'notes', 'assigned_to', 'asset_location'
)
depth = 1
read_only_fields = ("uuid",)


View



class ManageAssetViewSet(ModelViewSet):
serializer_class = AssetSerializer
queryset = Asset.objects.all()
# permission_classes = [IsAuthenticated, IsAdminUser]
# authentication_classes = (FirebaseTokenAuthentication,)
http_method_names = ['get', 'post', 'put', 'delete']
filter_backends = (filters.DjangoFilterBackend,)
filterset_class = AssetFilter

def get_object(self):
queryset = Asset.objects.all()
obj = get_object_or_404(queryset, uuid=self.kwargs['pk'])
return obj


Model
Asset Model. Some fields have been ommited



class Asset(models.Model):
"""Stores all assets"""
uuid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
asset_code = models.CharField(
unique=True, null=True, blank=True, max_length=50)
serial_number = models.CharField(
unique=True, null=True, blank=True, max_length=50)
created_at = models.DateTimeField(auto_now_add=True, editable=False)
asset_location = models.ForeignKey('AndelaCentre', blank=True, editable=True, null=True,
on_delete=models.PROTECT)
purchase_date = models.DateField(
validators=[validate_date],
null=True, blank=True)
last_modified = models.DateTimeField(auto_now=True, editable=False)
assigned_to = models.ForeignKey('AssetAssignee',
blank=True,
editable=False,
null=True,
on_delete=models.PROTECT)
model_number = models.ForeignKey(AssetModelNumber,
null=True,
on_delete=models.PROTECT)
current_status = models.CharField(editable=False, max_length=50)
notes = models.TextField(editable=False, default=" ", )


However, on the browsable Api, only 4 fields are showing on the UPDATE/PUT form as shown in the diagram below
enter image description here



What could be the reason some of the other fields are not appearing here. What determines which fields are updatable??










share|improve this question




















  • 2




    Share model view and serializer
    – a_k_v
    Nov 8 at 9:59










  • Just added them
    – Esir Kings
    Nov 8 at 10:03










  • add model details?
    – a_k_v
    Nov 8 at 10:07










  • Just added them
    – Esir Kings
    Nov 8 at 10:11













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I currently have a serializer with the following fields



class Meta:
model = Asset
fields = ('id', 'uuid', 'asset_category', 'asset_sub_category',
'make_label',
'asset_code', 'serial_number', 'model_number',
'checkin_status', 'created_at',
'last_modified', 'current_status', 'asset_type',
'allocation_history', 'specs', 'purchase_date',
'notes', 'assigned_to', 'asset_location'
)


Serializer



class AssetSerializer(serializers.ModelSerializer):
checkin_status = serializers.SerializerMethodField()
allocation_history = serializers.SerializerMethodField()
assigned_to = UserSerializer(read_only=True)
asset_category = serializers.SerializerMethodField()
asset_sub_category = serializers.SerializerMethodField()
make_label = serializers.SerializerMethodField()
asset_type = serializers.SerializerMethodField()
model_number = serializers.SlugRelatedField(
queryset=AssetModelNumber.objects.all(),
slug_field="model_number"
)

class Meta:
model = Asset
fields = ('id', 'uuid', 'asset_category', 'asset_sub_category',
'make_label',
'asset_code', 'serial_number', 'model_number',
'checkin_status', 'created_at',
'last_modified', 'current_status', 'asset_type',
'allocation_history', 'specs', 'purchase_date',
'notes', 'assigned_to', 'asset_location'
)
depth = 1
read_only_fields = ("uuid",)


View



class ManageAssetViewSet(ModelViewSet):
serializer_class = AssetSerializer
queryset = Asset.objects.all()
# permission_classes = [IsAuthenticated, IsAdminUser]
# authentication_classes = (FirebaseTokenAuthentication,)
http_method_names = ['get', 'post', 'put', 'delete']
filter_backends = (filters.DjangoFilterBackend,)
filterset_class = AssetFilter

def get_object(self):
queryset = Asset.objects.all()
obj = get_object_or_404(queryset, uuid=self.kwargs['pk'])
return obj


Model
Asset Model. Some fields have been ommited



class Asset(models.Model):
"""Stores all assets"""
uuid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
asset_code = models.CharField(
unique=True, null=True, blank=True, max_length=50)
serial_number = models.CharField(
unique=True, null=True, blank=True, max_length=50)
created_at = models.DateTimeField(auto_now_add=True, editable=False)
asset_location = models.ForeignKey('AndelaCentre', blank=True, editable=True, null=True,
on_delete=models.PROTECT)
purchase_date = models.DateField(
validators=[validate_date],
null=True, blank=True)
last_modified = models.DateTimeField(auto_now=True, editable=False)
assigned_to = models.ForeignKey('AssetAssignee',
blank=True,
editable=False,
null=True,
on_delete=models.PROTECT)
model_number = models.ForeignKey(AssetModelNumber,
null=True,
on_delete=models.PROTECT)
current_status = models.CharField(editable=False, max_length=50)
notes = models.TextField(editable=False, default=" ", )


However, on the browsable Api, only 4 fields are showing on the UPDATE/PUT form as shown in the diagram below
enter image description here



What could be the reason some of the other fields are not appearing here. What determines which fields are updatable??










share|improve this question















I currently have a serializer with the following fields



class Meta:
model = Asset
fields = ('id', 'uuid', 'asset_category', 'asset_sub_category',
'make_label',
'asset_code', 'serial_number', 'model_number',
'checkin_status', 'created_at',
'last_modified', 'current_status', 'asset_type',
'allocation_history', 'specs', 'purchase_date',
'notes', 'assigned_to', 'asset_location'
)


Serializer



class AssetSerializer(serializers.ModelSerializer):
checkin_status = serializers.SerializerMethodField()
allocation_history = serializers.SerializerMethodField()
assigned_to = UserSerializer(read_only=True)
asset_category = serializers.SerializerMethodField()
asset_sub_category = serializers.SerializerMethodField()
make_label = serializers.SerializerMethodField()
asset_type = serializers.SerializerMethodField()
model_number = serializers.SlugRelatedField(
queryset=AssetModelNumber.objects.all(),
slug_field="model_number"
)

class Meta:
model = Asset
fields = ('id', 'uuid', 'asset_category', 'asset_sub_category',
'make_label',
'asset_code', 'serial_number', 'model_number',
'checkin_status', 'created_at',
'last_modified', 'current_status', 'asset_type',
'allocation_history', 'specs', 'purchase_date',
'notes', 'assigned_to', 'asset_location'
)
depth = 1
read_only_fields = ("uuid",)


View



class ManageAssetViewSet(ModelViewSet):
serializer_class = AssetSerializer
queryset = Asset.objects.all()
# permission_classes = [IsAuthenticated, IsAdminUser]
# authentication_classes = (FirebaseTokenAuthentication,)
http_method_names = ['get', 'post', 'put', 'delete']
filter_backends = (filters.DjangoFilterBackend,)
filterset_class = AssetFilter

def get_object(self):
queryset = Asset.objects.all()
obj = get_object_or_404(queryset, uuid=self.kwargs['pk'])
return obj


Model
Asset Model. Some fields have been ommited



class Asset(models.Model):
"""Stores all assets"""
uuid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
asset_code = models.CharField(
unique=True, null=True, blank=True, max_length=50)
serial_number = models.CharField(
unique=True, null=True, blank=True, max_length=50)
created_at = models.DateTimeField(auto_now_add=True, editable=False)
asset_location = models.ForeignKey('AndelaCentre', blank=True, editable=True, null=True,
on_delete=models.PROTECT)
purchase_date = models.DateField(
validators=[validate_date],
null=True, blank=True)
last_modified = models.DateTimeField(auto_now=True, editable=False)
assigned_to = models.ForeignKey('AssetAssignee',
blank=True,
editable=False,
null=True,
on_delete=models.PROTECT)
model_number = models.ForeignKey(AssetModelNumber,
null=True,
on_delete=models.PROTECT)
current_status = models.CharField(editable=False, max_length=50)
notes = models.TextField(editable=False, default=" ", )


However, on the browsable Api, only 4 fields are showing on the UPDATE/PUT form as shown in the diagram below
enter image description here



What could be the reason some of the other fields are not appearing here. What determines which fields are updatable??







python django django-rest-framework django-rest-viewsets






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 10:11

























asked Nov 8 at 9:41









Esir Kings

643421




643421








  • 2




    Share model view and serializer
    – a_k_v
    Nov 8 at 9:59










  • Just added them
    – Esir Kings
    Nov 8 at 10:03










  • add model details?
    – a_k_v
    Nov 8 at 10:07










  • Just added them
    – Esir Kings
    Nov 8 at 10:11














  • 2




    Share model view and serializer
    – a_k_v
    Nov 8 at 9:59










  • Just added them
    – Esir Kings
    Nov 8 at 10:03










  • add model details?
    – a_k_v
    Nov 8 at 10:07










  • Just added them
    – Esir Kings
    Nov 8 at 10:11








2




2




Share model view and serializer
– a_k_v
Nov 8 at 9:59




Share model view and serializer
– a_k_v
Nov 8 at 9:59












Just added them
– Esir Kings
Nov 8 at 10:03




Just added them
– Esir Kings
Nov 8 at 10:03












add model details?
– a_k_v
Nov 8 at 10:07




add model details?
– a_k_v
Nov 8 at 10:07












Just added them
– Esir Kings
Nov 8 at 10:11




Just added them
– Esir Kings
Nov 8 at 10:11












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










Well, the problem is when you set depth = 1 ModelSerializer tries to generate a NestedSerializer field for any foreignkey related field which you have not explicitly mentioned. And that NestedSerializer field is a read only field. That's why Assest Location is not being displayed. Remove that depth = 1 line and DRF will map the said field with the default mapping i.e. PrimaryKeyRelatedFiel and you will see that the said field is displayed.






share|improve this answer





















  • thanks for this
    – Esir Kings
    Nov 8 at 19:50










  • No problem bro!!
    – dipen bhatt
    Nov 9 at 20:02


















up vote
0
down vote













In your model, you make editable=False for many fields. That fields won't display. If you want to display and edit that field remove that option.
For more info refer question






share|improve this answer





















  • Asset location has editable as true but still isn't displaying
    – Esir Kings
    Nov 8 at 10:43











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205047%2fwhat-determines-what-fields-can-be-updated-in-djangorestframework%23new-answer', 'question_page');
}
);

Post as a guest
































2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Well, the problem is when you set depth = 1 ModelSerializer tries to generate a NestedSerializer field for any foreignkey related field which you have not explicitly mentioned. And that NestedSerializer field is a read only field. That's why Assest Location is not being displayed. Remove that depth = 1 line and DRF will map the said field with the default mapping i.e. PrimaryKeyRelatedFiel and you will see that the said field is displayed.






share|improve this answer





















  • thanks for this
    – Esir Kings
    Nov 8 at 19:50










  • No problem bro!!
    – dipen bhatt
    Nov 9 at 20:02















up vote
1
down vote



accepted










Well, the problem is when you set depth = 1 ModelSerializer tries to generate a NestedSerializer field for any foreignkey related field which you have not explicitly mentioned. And that NestedSerializer field is a read only field. That's why Assest Location is not being displayed. Remove that depth = 1 line and DRF will map the said field with the default mapping i.e. PrimaryKeyRelatedFiel and you will see that the said field is displayed.






share|improve this answer





















  • thanks for this
    – Esir Kings
    Nov 8 at 19:50










  • No problem bro!!
    – dipen bhatt
    Nov 9 at 20:02













up vote
1
down vote



accepted







up vote
1
down vote



accepted






Well, the problem is when you set depth = 1 ModelSerializer tries to generate a NestedSerializer field for any foreignkey related field which you have not explicitly mentioned. And that NestedSerializer field is a read only field. That's why Assest Location is not being displayed. Remove that depth = 1 line and DRF will map the said field with the default mapping i.e. PrimaryKeyRelatedFiel and you will see that the said field is displayed.






share|improve this answer












Well, the problem is when you set depth = 1 ModelSerializer tries to generate a NestedSerializer field for any foreignkey related field which you have not explicitly mentioned. And that NestedSerializer field is a read only field. That's why Assest Location is not being displayed. Remove that depth = 1 line and DRF will map the said field with the default mapping i.e. PrimaryKeyRelatedFiel and you will see that the said field is displayed.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 8 at 19:33









dipen bhatt

617




617












  • thanks for this
    – Esir Kings
    Nov 8 at 19:50










  • No problem bro!!
    – dipen bhatt
    Nov 9 at 20:02


















  • thanks for this
    – Esir Kings
    Nov 8 at 19:50










  • No problem bro!!
    – dipen bhatt
    Nov 9 at 20:02
















thanks for this
– Esir Kings
Nov 8 at 19:50




thanks for this
– Esir Kings
Nov 8 at 19:50












No problem bro!!
– dipen bhatt
Nov 9 at 20:02




No problem bro!!
– dipen bhatt
Nov 9 at 20:02












up vote
0
down vote













In your model, you make editable=False for many fields. That fields won't display. If you want to display and edit that field remove that option.
For more info refer question






share|improve this answer





















  • Asset location has editable as true but still isn't displaying
    – Esir Kings
    Nov 8 at 10:43















up vote
0
down vote













In your model, you make editable=False for many fields. That fields won't display. If you want to display and edit that field remove that option.
For more info refer question






share|improve this answer





















  • Asset location has editable as true but still isn't displaying
    – Esir Kings
    Nov 8 at 10:43













up vote
0
down vote










up vote
0
down vote









In your model, you make editable=False for many fields. That fields won't display. If you want to display and edit that field remove that option.
For more info refer question






share|improve this answer












In your model, you make editable=False for many fields. That fields won't display. If you want to display and edit that field remove that option.
For more info refer question







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 8 at 10:15









a_k_v

57112




57112












  • Asset location has editable as true but still isn't displaying
    – Esir Kings
    Nov 8 at 10:43


















  • Asset location has editable as true but still isn't displaying
    – Esir Kings
    Nov 8 at 10:43
















Asset location has editable as true but still isn't displaying
– Esir Kings
Nov 8 at 10:43




Asset location has editable as true but still isn't displaying
– Esir Kings
Nov 8 at 10:43


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205047%2fwhat-determines-what-fields-can-be-updated-in-djangorestframework%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff