I need to send some extra information in the get api let say first_name and last_name that are non fields for model in the serializer Django Rest Framework

Submitted 1 year, 10 months ago
Ticket #33
Views 120
Language Other
Priority Urgent
Status Closed

Waiter Task Model

'''

class WaiterTasks(models.Model):
    task_id = models.AutoField(primary_key=True)
    admin = models.ForeignKey(Admins, models.DO_NOTHING, blank=True, null=True)
    waiter = models.ForeignKey('Waiters', models.DO_NOTHING, blank=True, null=True)
    task_description = models.CharField(max_length=50, blank=True, null=True)
    date = models.DateTimeField(db_column='Date',auto_now_add=True)  # Field name made lowercase.

    def __str__(self):
        return self.task_id

    class Meta:
        managed = False
        db_table = 'waiter_tasks'

'''

""

class WaiterTasksSerializer(serializers.ModelSerializer):

    class Meta:
        model=WaiterTasks
        fields=['task_id','admin','waiter','task_description','date','admin_first_name','admin_last_name']

"""

in views.py

"""

class WaiterTaskslist(APIView):

    def get(self,request):
        taskslist=WaiterTasks.objects.all()
        serializer=WaiterTasksSerializer(taskslist, many=True)
        serializer.admin_first_name='Moazam'
        serializer.admin_last_name='Ali'
        return Response(serializer.data)
    def post(self):
        pass

"""

Submitted on Jul 03, 20
add a comment

1 Answer

you can add something like this in your serializers.py username = CharField(source='user.username', read_only=True) user_image = ImageField( source='user.profile.image', read_only=True) Then add this field in field =[]

Submitted 1 year, 10 months ago

user_image = ImageField( source='user.profile.image', read_only=True)

class Meta:
    model = Question
    fields = '__all__'
- arun 1 year, 10 months ago


Wanna Post Ad?
Reach a large group of audience by posting an ad about your business here. For more details
Drop us a Message