Full Address

43 views
Skip to first unread message

Softtar

unread,
Oct 18, 2020, 4:39:58 AM10/18/20
to Django REST framework
I want to show full address in one row I have four address fields How i can I do that please.
I tried below code it's not working

class Meta:
model = Application
fields = ('id', 'address1 + address2 + address3 + addres4','status')

Jakob Damgaard Møller

unread,
Oct 18, 2020, 4:53:44 AM10/18/20
to django-res...@googlegroups.com

--
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-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/93ceeb6a-c7c1-4c7b-834d-81429ef4b0f8n%40googlegroups.com.
--
Venlig hilsen
Jakob Damgaard Møller
Mobil: 24613112

Agnese Camellini

unread,
Oct 18, 2020, 4:56:48 AM10/18/20
to django-res...@googlegroups.com
You have to put the aphex (') around each word not just at the beginning and at end of the instruction....

--

Etémé Tsanga R.

unread,
Oct 18, 2020, 5:00:27 AM10/18/20
to django-res...@googlegroups.com
class Meta:
  model = Application
   fields = ('id', 'address1 + address2 + address3 + addres4','status')

def to_representation(self, instance):
    response_dict = dict()
    response_dict['id'] = instance.id
    response_dict['status'] = instance.status
    response_dict['address'] = f"{instance.address1}- {instance.address2}
 -{instance.address3}-{instance.address4}"
    return response_dict

--
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-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/93ceeb6a-c7c1-4c7b-834d-81429ef4b0f8n%40googlegroups.com.


--
Etémé T.  | Software architecture
Tel : +237 697-414-835
        +237 674-507-065
E-mail :  ete...@yahoo.fr











Etémé Tsanga R.

unread,
Oct 18, 2020, 5:01:58 AM10/18/20
to django-res...@googlegroups.com
class Meta:
  model = Application
   fields = ('id', 'address1','address2','address3','addres4','status')

def to_representation(self, instance):
    response_dict = dict()
    response_dict['id'] = instance.id
    response_dict['status'] = instance.status
    response_dict['address'] = f"{instance.address1}- {instance.address2}
 -{instance.address3}-{instance.address4}"
    return response_dict
Le dim. 18 oct. 2020 à 09:40, Softtar <soft...@gmail.com> a écrit :
--
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-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/93ceeb6a-c7c1-4c7b-834d-81429ef4b0f8n%40googlegroups.com.

Softtar

unread,
Oct 18, 2020, 7:15:11 AM10/18/20
to django-res...@googlegroups.com
class Meta:
model = Application
    fields = ('id', 'address1', 'address2', 'address3', 'status')


def to_representation(self, instance):
response_dict = dict()
response_dict['id'] = instance.id
response_dict['status'] = instance.status
response_dict['address'] = f"{instance.address1}- {instance.address2} -{instance.address3} - {instance.address4}"
return response_dict
I tired that and it's showing me address still in four rows instead of one row.

You received this message because you are subscribed to a topic in the Google Groups "Django REST framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/B71JQxvpRdE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-rest-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/CA%2BBWBj3eHjtt%2BYSMU8DyKF4SBdZ2i0h3KZME1WR_LFTDcQmQeg%40mail.gmail.com.

Jakob Damgaard Møller

unread,
Oct 18, 2020, 8:17:11 AM10/18/20
to django-res...@googlegroups.com
Try this one:

class YourSerializer(serializers.ModelSerializer):
    full_address = serializers.SerializerMethodField()


    class Meta:
        model = Application
        fields = ('id', 'full_address','status')
   
    def get_full_address(self, obj):
        return f"{address1} {address2} {address3} {addres4}"

Softtar

unread,
Oct 18, 2020, 9:07:53 AM10/18/20
to django-res...@googlegroups.com
class ListAppsSerializer(serializers.ModelSerializer):

full_address = serializers.SerializerMethodField()

class Meta:
model = Application
fields = ('id', 'full_address', 'status')

def get_full_address(self, obj):
return f"{address1} {address2} {address3} {addres4}"
it's saying address1 address2 address3 and address4 is not define

Jakob Damgaard Møller

unread,
Oct 18, 2020, 9:12:33 AM10/18/20
to django-res...@googlegroups.com
Add obj. In front of all four chases.

Softtar

unread,
Oct 18, 2020, 9:15:48 AM10/18/20
to django-res...@googlegroups.com
Yes Thanks Sir. It's working Now. Thanks again Sir. I have one more question

I want to pass condition base parameters in url how i can do that
path('api/filterapplication/<cnic> || <mobile>', ApplicationLIST1.as_view()),

def get(self, request, cnic, mobile):
snippets = Application.objects.filter(cnic=cnic) | Application.objects.filter(mobile=mobile)
serializer = ApplicationSerializer(snippets, many=True)
return Response(serializer.data)





Matemática A3K

unread,
Oct 18, 2020, 9:21:37 PM10/18/20
to django-res...@googlegroups.com
On Sun, Oct 18, 2020 at 8:15 AM Softtar <soft...@gmail.com> wrote:
Yes Thanks Sir. It's working Now. Thanks again Sir. I have one more question

I want to pass condition base parameters in url how i can do that
path('api/filterapplication/<cnic> || <mobile>', ApplicationLIST1.as_view()),

def get(self, request, cnic, mobile):
snippets = Application.objects.filter(cnic=cnic) | Application.objects.filter(mobile=mobile)
serializer = ApplicationSerializer(snippets, many=True)
return Response(serializer.data)






That syntax in path() is not supported:

One way is having two endpoints, one for cnic and other for mobile, another is using GET parameters (url resource filtering)
You can find the rationales in the Internet ("REST API design"), also take a look at the resources listed here:
 
Reply all
Reply to author
Forward
0 new messages