英文:
foregin key using name value fetch in django rest frame work
问题
我已添加了model.py、view.py和serializer.py文件,我将分享国家和州的模型。我将在国家和州字段上应用外键。我将在Django Rest Framework的HTML字段中仅获取对象ID而不是值,就像城市(Morbi)一样。
[
英文:
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).
[
答案1
得分: 0
Override the __str__()
function in the 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
You can do the same with the state too.
英文:
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.
答案2
得分: 0
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.
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论