我在Django新建两个model,一个class Account,一个是class Person,并且person = models.
models.OneToOneField(Account),那我在html表单里在知道account的情况下怎么调用Person里的域?
Django的html表单的数据库查询问题
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
你知我知皆知 2024-08-04 09:11关注以下回答参考 皆我百晓生、券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。
你可以使用
reverse()方法来获取模型的URL。例如:from django.urls import reverse def my_view(request): # Get the account object from request account = Account.objects.get(pk=request.GET['account_id']) # Get the person object by its relationship to the account person = Person.objects.get(account=account) # Return a JSON response with the person's details return HttpResponse(person.to_json(), content_type="application/json")在这个例子中,我们首先从请求中获取账户对象,并通过其ID获取到Person对象。然后返回包含Person对象JSON格式的数据。
请注意,你需要根据你的具体需求调整这个代码。例如,你可能需要处理不同的关系类型(比如一对一、一对多等),或者你可能需要将Person对象转换为JSON格式。
解决 无用评论 打赏 举报