On Fri, Oct 16, 2020 at 6:17 AM Andy922 <zeroseve...@gmail.com> wrote:

> I managed to get this to work, perhaps a suggested improvement is to
> re-write this into an "easier" syntax on the same form as other serializer
> fields.
>
> Model is called "Product"
>
> Length = serializers.ModelField(model_field=Product()._meta.get_field('
> RTF_44534_PB'), source='RTF_44534_PB')
>
>
A quick draft (without error handling) could be:

from django.apps import apps
from rest_framework import serializers

class SModelField(serializers.ModelField):

    def __init__(self, *args, **kwargs):
        if isinstance(kwargs['model_field'], str):
            app_label, model, field = kwargs['model_field'].split(".")
            Model = apps.get_model(app_label, model)
            kwargs['model_field'] = Model()._meta.get_field(field)
        super().__init__(*args, **kwargs)

then you'll "Length =
SModelField(model_field="product_app.Product.RTF_44534_PB")" and should
work :)


> On Friday, October 16, 2020 at 10:50:22 AM UTC+1 Andy922 wrote:
>
>> Hi, I'd like to ask how you amend your field names.
>>
>> I have encountered two "problems" I'd like to ask about.
>>
>> A. Is there a function to quickly re-name model-fields in bulk?
>> B. How do you re-name a foreign-key?
>>
>> I assume that 95% of all users change the field names, as the python
>> convention is different to that of e.g. json, and that internal model
>> representation is normally not very user friendly.
>>
>> I have tried on my own, but soon end up in a conclusion that I am totally
>> wrong about the task, when starting to work with completely re-writing over
>> 100 field names within the serializer when I just want to rename it for the
>> user.
>>
>> e.g. if I have an internal model fieldname called RTF_44534_PB =
>> models.DecimalField(decimal_places=2, max_digits=5, blank=True, null=True
>> )
>>
>> I re-write it as
>>
>> Length = serializers.DecimalField(decimal_places=2, max_digits=5,
>> source="RTF_44534_PB")
>> If it would exist a serializer field on the form:, Length =
>> serializers.ModelField(RTF_44534_PB), would considerable speed up
>> everything and minimize any potential mismatches.
>>
>> This manual method works on all fields but when I come to a foreginkey, I
>> cannot find how to re-name that field in a similar way. How do yo re-name a
>> foreign key?
>>
>> Many thanks
>>
>> Andy
>>
>>
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Django REST framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-rest-framework+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-rest-framework/2dde85db-7daf-4f89-a8bd-08ca9013d598n%40googlegroups.com
> <https://groups.google.com/d/msgid/django-rest-framework/2dde85db-7daf-4f89-a8bd-08ca9013d598n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-rest-framework/CA%2BFDnh%2BgQSZqv7jJgf91OuaEa_Qt4TZQTCx1_AzB0ehb7i_Jvg%40mail.gmail.com.

Reply via email to