Django 管理更改列表以显示空外键(向后)

发布于 2024-10-18 09:24:38 字数 825 浏览 0 评论 0原文

假设两个模型有一个标准的ForeignKey,例如:

Company(models.Model):
    name = models.CharField('Nome', max_length = 255)
    ...

Ticket(models.Model):
    company = ForeignKey(Company)
    ...

-- Update

在这个例子中,我的数据库中已经有很多公司,但没有Ticket。考虑到这一点,我的概念可能是错误的。但无论如何,我们开始......

在应用程序的 admin.py 中:

class TicketAdmin(admin.ModelAdmin):
    # ...

    def queryset(self,request):
        # ...

        # This gives me the base I need: Companies without tickets
        companies = Company.objects.annotate(n = Count('ticket')).filter(n = 0)

        # But as the Ticket objects don't exist for a Company, I can't get them by a standard filter like:
        Ticket.objects.filter(company__in = [o.id for o in companies])

我怎样才能进行这样的查询?

好吧...希望我现在已经足够清楚了。

Assume a standard ForeignKey for two models, like:

Company(models.Model):
    name = models.CharField('Nome', max_length = 255)
    ...

Ticket(models.Model):
    company = ForeignKey(Company)
    ...

-- Update

In this example, I have a lot of companies already in the database, but no Ticket. Considering this, I might be wrong in concept. But here we go anyway...

In admin.py of the app:

class TicketAdmin(admin.ModelAdmin):
    # ...

    def queryset(self,request):
        # ...

        # This gives me the base I need: Companies without tickets
        companies = Company.objects.annotate(n = Count('ticket')).filter(n = 0)

        # But as the Ticket objects don't exist for a Company, I can't get them by a standard filter like:
        Ticket.objects.filter(company__in = [o.id for o in companies])

How can I make a query like this?

Well... hope I was clear enough now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七秒鱼° 2024-10-25 09:24:39

你有没有尝试过:

Company.objects.filter(ticket_set=None)

Have you tried:

Company.objects.filter(ticket_set=None)
温暖的光 2024-10-25 09:24:39
Company.objects.annotate(tickets=Count('ticket')).filter(tickets=0)

根据您的评论进行更新:

要在管理中显示此内容,

  • 请为此模型创建一个 ModelForm
  • 在此 ModelForm 中包含一个新的 ModelChoiceField 以其中,您传递相关查询集和 Company 模型。
  • 在此模型的 ModelAdmin 中,传递表单的参数 - 上面创建的 ModelForm
  • 无论您想对所选公司执行什么操作,都可以忽略工单 ModelForm 上的保存 并执行操作。
Company.objects.annotate(tickets=Count('ticket')).filter(tickets=0)

Update, based on your comment:

To display this in the admin,

  • Create a ModelForm, for this model
  • In this ModelForm Include a new ModelChoiceField to which, you pass the relevant queryset and the Company model.
  • In the ModelAdmin for this model, pass the argument for the form - the ModelForm created above.
  • Whatever you want to do with the Company selected, over ride the save on the Ticket ModelForm and do.
浪荡不羁 2024-10-25 09:24:39

我找到的解决方案:

在 urls.conf 中,添加模式:

(r'^main/ligacao/

然后,在 admin_views.py 中,创建 Ticket_list_result 方法

from django.template import RequestContext
from django.shortcuts import render_to_response
from django.db.models import Count

def ticket_list_result (request):

    # ... parses, fetches, etc...

    # Get companies without tickets
    companies = Company.objects.annotate(n = Count('ticket')).filter(n = 0)

    results = list()    
    for c in companies:
        # Get tickets for the company
        tickets = Ticket.objects.filter(company = c.id)

        # Construct a dict to return to the view 
        results.append({
            'company': c,
            # ... any other desired info
        })

    return render_to_response(
        "admin/view_template.html",
        {'results' : results },
        RequestContext(request, {}),
    )

并在“admin/view_template.html”视图中列出这些结果:

{% block result_list %}

    <table cellspacing="0" id="result_list">

        <thead>
            <tr>
                <th>Company</th>
                <!-- other columns -->
            </tr>
        </thead>

        <tbody>
            {% for r in results %}
                <tr class="{% cycle 'row1' 'row2' %}">
                    <th>{{ r.company }}</th>
                    <!-- other company info -->
                </tr>
            {% endfor %}
        </tbody>

    </table>

{% endblock %}
, 'main.admin_views.ticket_list_result')

然后,在 admin_views.py 中,创建 Ticket_list_result 方法

并在“admin/view_template.html”视图中列出这些结果:

The solution I found:

In urls.conf, add the pattern:

(r'^main/ligacao/

Then, in admin_views.py, create the ticket_list_result method

from django.template import RequestContext
from django.shortcuts import render_to_response
from django.db.models import Count

def ticket_list_result (request):

    # ... parses, fetches, etc...

    # Get companies without tickets
    companies = Company.objects.annotate(n = Count('ticket')).filter(n = 0)

    results = list()    
    for c in companies:
        # Get tickets for the company
        tickets = Ticket.objects.filter(company = c.id)

        # Construct a dict to return to the view 
        results.append({
            'company': c,
            # ... any other desired info
        })

    return render_to_response(
        "admin/view_template.html",
        {'results' : results },
        RequestContext(request, {}),
    )

And list these results in the view on "admin/view_template.html":

{% block result_list %}

    <table cellspacing="0" id="result_list">

        <thead>
            <tr>
                <th>Company</th>
                <!-- other columns -->
            </tr>
        </thead>

        <tbody>
            {% for r in results %}
                <tr class="{% cycle 'row1' 'row2' %}">
                    <th>{{ r.company }}</th>
                    <!-- other company info -->
                </tr>
            {% endfor %}
        </tbody>

    </table>

{% endblock %}
, 'main.admin_views.ticket_list_result')

Then, in admin_views.py, create the ticket_list_result method

And list these results in the view on "admin/view_template.html":

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文