Dave Pradana
Dave Pradana

Reputation: 145

modifying queryset in django admin page for auth_users and a custom related model

So, I have a model DailyTask that has a OneToOneField to the auth_users model, this is the DailyTask model fields :

task_id

task_date

user ( OneToOneField )

Now each user has to submit a new Task report daily, already have an AdminModel that display all the submitted Tasks respectively to the logged in non superuser, here is the AdminModel :

class DailyTaskAdmin(admin.ModelAdmin):
    def get_queryset(self, request):
        qs = super().get_queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter(user=request.user)

and I have created a proxy Model so I can create and register another AdminModel using the same Model :

class MyDailyTask(DailyTask):
    class Meta:
        proxy = True

then the second AdminModel :

class DailyPresenceAdmin(DailyTaskAdmin):
    def get_queryset(self, request):
        qs = super().get_queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter()

admin.site.register(MyDailyTask, DailyPresenceAdmin)

My only problem is, that in the DailyPresenceAdmin AdminModel, want to list all the users from auth_users that has not uploaded their Task of the day yet.

so it will be something like,

if auth_users.user has no TaskID with the date equals to today's date in the DailyTask Model yet, then I want to take the user and list it in the DailyPresenceAdmin AdminModel.

Upvotes: 0

Views: 114

Answers (1)

Eby Sofyan
Eby Sofyan

Reputation: 464

You can perform a query from User model

User.objects.filter(mydailytask__task_date=timezone.today(), mydailytask__isnull=True)

Upvotes: 1

Related Questions