0

I have two models. One is 'Task' and the other one is 'Entry' and I am able to see an overview of the all entries made for each task in Django Admin panel. I would like to create a similar page for logged in user to display which entries he created in db.

I tried to get them by using log data with LogEntry class, but I stucked at it. Does somebody know an efficient way to do it?

I have the following models

class Profile(models.Model):
    # Relations
    user = models.OneToOneField(
        User,
        related_name="profile",
        verbose_name=_("user")
        )
    # Attributes - Mandatory
    interaction = models.PositiveIntegerField(
        default=0,
        verbose_name=_("interaction")
        )
    inter_time = models.DateTimeField(auto_now=True)
    # Attributes - Optional
    # Object Manager
    objects = managers.ProfileManager()

    # Custom Properties
    @property
    def username(self):
        return self.user.username

    # Meta and String
    class Meta:
        verbose_name = _("Profile")
        verbose_name_plural = _("Profiles")
        ordering = ("user",)

    def __str__(self):
        return self.user.username


#@receiver(post_save, sender=settings.AUTH_USER_MODEL)
@receiver(post_save, sender=User)
def create_profile_for_new_user(sender, created, instance, **kwargs):
    if created:
        profile = Profile(user=instance)
        profile.save()

def login_user(sender, request, user, **kwargs):
    user.profile.interaction += 1
    user.profile.save()

user_logged_in.connect(login_user)


class Task(models.Model):
    task_name = models.CharField(max_length=250)
    task_time = models.DecimalField(max_digits=3, decimal_places=2)

    def __unicode__(self):
            return self.task_name

    # Object Manager
    #objects = managers.TaskManager()

    # Meta and String
    class Meta:
        verbose_name = _("Task")
        verbose_name_plural = _("Tasks")


class Entry(models.Model):
    select_task = models.ForeignKey(Task, related_name="entries")
    entry_quantity = models.IntegerField(default=0)
    entry_date = models.DateField('Task performed date')
    publication_date = models.DateField(auto_now_add=True)

    def __unicode__(self):
        return u'%s' % self.entry_quantity

    # Object Manager
    #objects = managers.EntryManager()

    # Meta and String
    class Meta:
        verbose_name = _("Entry")
        verbose_name_plural = _("Entries")

This is what I have in Admin. I want to have something similar for each user (will show the entries made by that user only) in the user profile page. I had to cross over the tasks since the product doesn't belong to me.

enter image description here

Rikkas
  • 572
  • 4
  • 19

2 Answers2

1

Something like this?

# in admins.YourModelAdmin
def get_queryset(self, request):
    qs = super().get_queryset(request)
    content_type = ContentType.objects.get_for_model(Entry)
    created_by_user = LogEntry.objects.filter(user=request.user, content_type = content_type).values_list('pk', flat = True)
    return qs.filter(pk__in=created_by_user)

May have to fiddle with some bits and bobs; but this should get you the Entry objects created by the currently logged in user. For more information, please read get_queryset and ContentType.
If you want to include the user that has created a certain Entry in your changelist, refer to list_disply.

CoffeeBasedLifeform
  • 2,296
  • 12
  • 27
0

Check to see if you have an admin.py in your app. That is where all of the Django Admin entries are defined. You likely have admin entries for Tag and Entry there. If you want one for Profile, add that there.

aredzko
  • 1,690
  • 14
  • 14
  • please see the added screenshot. I already have them listed in admin. Do you know a way to add the user who makes the entry to the admin view? With the similar logic, I can then filter it by user and try to figure out to list it similarly for active users in a profile page. – Rikkas Jun 29 '18 at 15:24
  • You'd need to track that on the model. Once you do that, you can modify the admin view to show that field in the list view (`list_display`). – aredzko Jun 29 '18 at 17:18