Home > Net >  Django ordering by date_created with ManyToMany using Through
Django ordering by date_created with ManyToMany using Through

Time:06-21

Having trouble figuring out how to order by date created. I've looked at some similar problems on here and no fix. I need the dates to order by ascending so that the most recent object created is on the top of the list. I've tried different things even using JS to revert the list and still no luck.

I have:

class Info(models.Model):
    detail = models.CharField(max_length=50)
    text = models.TextField(max_length=2000)
    created_by = models.ForeignKey(User, on_delete=models.SET_NULL, blank=True, null=True, related_name='cb_section')
    updated_by = models.ForeignKey(User, on_delete=models.SET_NULL, blank=True, null=True, related_name='up_section')
    date_created = models.DateField('Date created', auto_now_add=True)
    date_updated = models.DateField('Date updated', auto_now=True)

    

class Section(models.Model):
    name = models.CharField(max_length=20)
    section_infos = models.ManyToManyField(Info, through='SectionInfo')
    date_created = models.DateField('Date created', auto_now_add=True)
    date_updated = models.DateField('Date updated', auto_now=True)
    def __str__(self):
        return self.name
    def get_section_info(self):
        return self.section_infos.order_by('-date_created')




class SectionInfo(models.Model):
    info = models.ForeignKey(Info, on_delete=models.CASCADE)
    section = models.ForeignKey(Section, on_delete=models.CASCADE)

    class Meta:
        ordering = ('info__date_created',)

and in my template I have

<div >
    {% for object in object_list %}
    <div >
        <div >
            <div >
                <h1><strong>{{ object.name }}</strong></h1>
                <hr>
            </div>
            <div > 
                <div >       
                    {% for info in object.section_infos.all %}
                    
                    <ul id="list">
                        <li>{{ info.date_created }}</li> |
                        <li><a href="{% url 'manufacturing:section_info_detail' info.id %}">{{ info.detail }}</a></li>
                        <hr>
                    </ul>
                {% endfor %}
                </div>
            </div>
        </div>
    </div>
    {% endfor %}
</div>

View for this is

# view list of all sections and section infos
def view_sections(request):
    object_list = Section.objects.all().order_by('-date_created')

    context = {
        'object_list':object_list,
    }

    return render(request, 'manufacturing/view_sections.html', context)

CodePudding user response:

You can apply ordering=['-date_created'] in descending order in Info model which will fetch the latest date from date_created field.

models.py

class Info(models.Model):
    detail = models.CharField(max_length=50)
    text = models.TextField(max_length=2000)
    created_by = models.ForeignKey(User, on_delete=models.SET_NULL, blank=True, null=True, related_name='cb_section')
    updated_by = models.ForeignKey(User, on_delete=models.SET_NULL, blank=True, null=True, related_name='up_section')
    date_created = models.DateField('Date created', auto_now_add=True)
    date_updated = models.DateField('Date updated', auto_now=True)
    
    class Meta:
         ordering=['-date_created']
    

And remove ordering = ('info__date_created',) from SectionInfo model.

  • Related