开发者

Django admin change list to display null ForeignKeys (backwards)

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.


Have you tried:

Company.objects.filter(ticket_set=None)


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.


The solution I found:

In urls.conf, add the pattern:

(r'^main/ligacao/$', 'main.admin_views.ticket_list_result')

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 %}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜