class Meta:
model = Application
fields = (‘id’, ‘address1 + address2 + address3 + addres4’,‘status’)
I want to show all four address in one row how i can do that
class Meta:
model = Application
fields = (‘id’, ‘address1 + address2 + address3 + addres4’,‘status’)
I want to show all four address in one row how i can do that
I think we’re going to need more context, information, and detail about what your issue is, because in general, if you’re using a serializer on a class with all fields specified, you will get all the data in that class.
I agree with Ken, you need to provide more context. I can however show you how you can create a custom serializer method field.
Assuming you have a model which looks something like this:
class MyModel(models.Model):
address1 = TextField
address2 = TextField
address3 = TextField
# so on and so forth
You could add a model method to return a string concatenating all the address.
class MyModel(models.Model):
address1 = TextField
address2 = TextField
address3 = TextField
# so on and so forth
def concat_addresses(self):
return self.address1 + self.address2 + self.address3
In your serialiser you can use
from rest_framework import serializers
class MySerializer(serializers.ModelSerializer):
model = MyModel
# you don't have to use the method_name parameter. If you don't, you must use
# a method name of 'get_` + `attribute
# in your case this would be get_address()
address = serializers.SerializerMethodField(method_name="concat_addresses")
class Meta:
fields = ("id", "whatever_else", "address")
@staticmethod
def concat_addresses(obj):
return obj.concat_addresses()
Checkout the serializer docs here: https://www.django-rest-framework.org/api-guide/fields/#serializermethodfield
Hope that helps you get started.
Cheers,
Conor