Django how to iterate querysets of two subclasses

2019-08-17 09:53发布

问题:

I am using django-model-utils for inheritance Managers. I want to get result of both sub-classes with one dictionary without getting duplicates.

models.py

class Images(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='images_created', on_delete=models.CASCADE)
    created = models.DateTimeField(auto_now_add=True, null=True, blank=True)
    objects = InheritanceManager()

class Postimg(Images):
    user_img= models.ImageField(upload_to='images/%Y/%m/%d', null=True, blank=True)


class Postsms(Images):
    user_message = models.CharField(max_length=500,blank=True)

views.py

def home(request):
    all_post = Images.objects.all().order_by('-created').select_subclasses()
    return render(request, 'copybook/home.html',{ 'all_post':all_post})

copybook/home.html

{% for foo in all_post %}
    <hr>
    <br> SMS by:  <p>{{ foo.user}}  __  {{ foo.created }}</p>
    <h2>{{ foo.user_message }}</h2>
    <hr>
    <br> IMAGE by:  <p>{{ foo.user}}  __  {{ foo.created }}</p> 
    <img src="{{ foo.user_img.url }}"width="250">
{% endfor %}

I expect the result when I upload an image or message that should get top at home page, but now when I upload an image, a blank message also gets iterated.

I think the problem is in my home.html, because I do not know how to iterate over two sub-classes with a single for loop without getting duplicates.

回答1:

In your template, you are processing every item as if it were both a message and an image. That's why you get empty image sections for messages and empty message sections for images.

The simplest workaround would be to check if user_img or user_message evaluates to True:

{% for foo in all_post %}
    <hr>
    {% if foo.user_message %}
        <br> SMS by:  <p>{{ foo.user}}  __  {{ foo.created }}</p>
        <h2>{{ foo.user_message }}</h2>
    {% else %}
        <br> IMAGE by:  <p>{{ foo.user}}  __  {{ foo.created }}</p> 
        <img src="{{ foo.user_img.url }}"width="250">
    {% endif %}
{% endfor %}

Instead of else you can do a separate if foo.user_img to avoid message objects with empty messages to be interpreted as images.