Custom Model Manager

10 views
Skip to first unread message

dtdave

unread,
May 25, 2017, 11:37:32 AM5/25/17
to Django users
I apologise if this is a simple question but I am still somewhat new to Dajngo.
I have the following models:

Jobs Model
Candidates Model
Applications Model

The jobs model has a manager on it as follows:
class ExpiredManager(models.Manager):
    def get_queryset(self):
        return super(ExpiredManager, self).get_queryset() \
         .filter(expiry_date__gt=datetime.now()).order_by('start_date')

And to make sure only unexpired items are listed on the web page my managers are listed as follows:
objects = ExpiredManager()
with_expired_objects = models.Manager()

Within my admin I have the following:
def get_queryset(self, request):
        """To return all jobs in admin including those that are expired"""
        qs = self.model.with_expired_objects.get_queryset()
        ordering = self.get_ordering(request)
        if ordering:
            qs = qs.order_by(*ordering)
        return qs
At this stage evrything works exactly as I want it to with all jobs being shown in the admin and only unexpired jobs on the web page.

My candidates model has the following field:
jobs = models.ManyToManyField('jobs.Job', through="applications.Application")

My application model has fields for candidate and jobs.
My problem is that once the job expires it is being dropped from both the candidates and the applications as obviously the expired items manager is the default.
Any help on how to correct this would be appreciated.
Many thanks


Melvyn Sopacua

unread,
May 25, 2017, 12:02:48 PM5/25/17
to django...@googlegroups.com

On Thursday 25 May 2017 08:37:32 'dtdave' via Django users wrote:

 

> My application model has fields for candidate and jobs.

> My problem is that once the job expires it is being dropped from both

> the candidates and the applications as obviously the expired items

> manager is the default.

> Any help on how to correct this would be appreciated.

 

The trick to Django is knowing where to find the relevant documentation bit.

--

Melvyn Sopacua

David Turner

unread,
May 25, 2017, 1:01:47 PM5/25/17
to django...@googlegroups.com
That would be fine if I was using 1.10 but this is a legacy system using 1.9. This will be upgraded in the foreseeable future to 1.11

Thanks

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/AjQXrIrkEjw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2567353.Uvk92CTyiI%40devstation.

For more options, visit https://groups.google.com/d/optout.

David Turner

unread,
May 25, 2017, 2:18:55 PM5/25/17
to django...@googlegroups.com
Apologies I was being really a bit too clever. I just need to put the deault manager first in my model and then use my custom manager in my view queryset and everything is fine.
Reply all
Reply to author
Forward
0 new messages