foregin key using name value fetch in django rest frame work

Question:

i have add model.py ,view.py and serilializer.py file ,i will share country and state model .
I wiil apply foregin key country and state fields.
I will check in html fields in django rest framework in only object id fetch not value so like city(Morbi).

htmlview

views.py

[model.pyserlializer.py

Asked By: Jyoti

||

Answers:

Override the __str__() function in model class to customize the dropdown correctly.

class Country(models.Model):
    # your data variables
    
    def __str__(self):
        return self.name # or any thing you want to show in dropdown

same you can do with state too.

Answered By: Vinay Vishwakarma

As Vinay has stated, add a return method to return some sort of detail to all your models. Also, make sure your model names start with a capital letter and there is no need to define the id as Django will always create that for you.

Answered By: Isaac Hatilima