基于类的视图查询:获取另一个模型引用的对象 [英] Class based views query: get objects referenced by another model

查看:108
本文介绍了基于类的视图查询:获取另一个模型引用的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题扩展了
Django将多个模型传递给一个模板



我想生成1个模板,即我的个人资料索引页,因此它将列出url <中指定的用户创建的所有对象/ p>

我想查询一个模板的多个模型,获取有问题的概要文件,并显示由附加概要文件创建的所有oferto对象



views.py

  class ProfileIndex(ListView):
context_object_name ='account_list'
template_name ='profiles / account_index.html'
queryset = Profile.objects.all()

def get_context_data(self,** kwargs):
context = super (ProfileIndex,自身).get_context_data(** kwargs)
context ['profile'] = Profile.objects.all()
context ['oferto'] = Oferto.objects.get(kwargs.profile .slug)

返回上下文

urls.py我想我需要从url中获取配置文件信息,以便通过网址中的该用户

  url(
regex = r ^ index /(?P< slug> [- _\w] +),
view = ProfileIndex.as_view(),
name = profile_index,
),

这是最难找出的,如何使我的模板从相关上下文中正常工作

  {%块内容%} 

< h1> {{个人资料}}< h1>
< h2> Ofertoj或/aŭOffer< / h2>
{%for aerto in profile%}
< a href = {%url oferto_detail oferto.slug%}> {{oferto.name}}< / a>< br />


{%endfor%}




{%endblock%}


解决方案

frontend / models.py

  Class Profile(models.Model):
....

Class Oferto(models.Model):
profile = model。 ForeignKey(配置文件):
...

frontend / views.py

  Class ViewProfileList(ListView):
模型=配置文件
template_name ='frontend / view_profile_list.html'

frontend / templates / frontend / view_profile_list.html

  {%为object_list.profile中的配置文件。所有%} 
{%为profile.oferto_set.all%中的销售者}
{{ofer.you_field}}
{ %endfor%}
{%endfor%}


this question extends Django Pass Multiple Models to one Template

In that I want to produce 1 template, that is my profile index page, so it will list all the objects created by the user specified in the url

I want to query multiple models for one template, get the Profile in question and show all the oferto objects that was created by the attached profile

views.py

class ProfileIndex(ListView):
context_object_name = 'account_list'    
template_name = 'profiles/account_index.html'
queryset = Profile.objects.all()

def get_context_data(self, **kwargs):
    context = super(ProfileIndex, self).get_context_data(**kwargs)
    context['profile'] = Profile.objects.all()
    context['oferto'] = Oferto.objects.get(kwargs.profile.slug)

    return context

urls.py I am thinking I need to get the profile slug from the url in order to search for all oferto by that user in the url

url(
regex=r"^index/(?P<slug>[-_\w]+)",
view=ProfileIndex.as_view(),
name="profile_index",
),

And this has been the hardest to figure out, how to make my template work correctly from the context in question

{% block content %}

<h1>{{ profile }}<h1>
    <h2> Ofertoj or/aŭ Offers</h2>
    {% for oferto in profile  %}
        <a href="{% url "oferto_detail" oferto.slug %}">{{ oferto.name }}</a><br/>


    {% endfor %}




{% endblock %}

解决方案

frontend/models.py

Class Profile(models.Model):
   ....

Class Oferto(models.Model): 
   profile = model.ForeignKey(Profile):
   ...

frontend/views.py

Class ViewProfileList(ListView):
    model = Profile
    template_name = 'frontend/view_profile_list.html'

frontend/templates/frontend/view_profile_list.html

  {% for profile in object_list.all %}
      {% for ofer in profile.oferto_set.all %}
         {{ ofer.you_field }}
      {% endfor %}
  {% endfor %} 

这篇关于基于类的视图查询:获取另一个模型引用的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆