0

I have trouble to display the ''saved''/''liked'' posts of my users in django/admin. I would like to have a field in the Adminpage to show which user likes which posts. I made an Userprofile model where all extra information (besides the one on the given django admin user Profile) are stored. so here is my model View:

class UserProfile(models.Model):
user = models.OneToOneField(User, null=True)
#likes = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True,default=1, related_name='likes')
likedPosts=models.ManyToManyField('self')
Field1 = models.CharField(max_length=50,default='Sunny')
Field2 = models.CharField(max_length=50,default='')

class Meta:
    ordering =['-user']

#def __unicode__(self):
 #   return self.user.username 


User.profile =property(lambda u: UserProfile.objects.get_or_create(user=u)[0])

right now in the liked post field I have only some usernames or "User object" I tried all kinds of combinations to get the information into the admin page but as you can see I did not make it. I tried to change the unicode and of course the liked post line. If you need more information please tell me so. I appreciate every kind of help.

hansTheFranz
  • 2,511
  • 4
  • 19
  • 35

2 Answers2

0

django admin isn't really meant to support many to many relationships from both directions in the django admin. However, the link below contains a workaround that I think should address your problem with a better explanation of why many-to-many relationships are only shown from one side by default.

(many-to-many in list display django).

Community
  • 1
  • 1
mokutsu
  • 11
  • 4
0

so for everybody who wants to do something similar this worked for me:

  class UserProfile(models.Model):
     likedPosts = models.ManyToManyField('self',default=None,blank=True)

def __unicode__(self):
    return "{0}".format(self.user.likes.all())
hansTheFranz
  • 2,511
  • 4
  • 19
  • 35