Search code examples
djangodjango-modelsdjango-admindjango-users

Why my model field is getting prepopulated in admin?


I have created a model as below.

class UserPost(models.Model):
    author = models.ForeignKey(User, related_name='userpost', null=True, on_delete=models.CASCADE)
    post_date = models.DateTimeField(auto_now_add=True)
    title = models.CharField(max_length=150, blank=False)
    post_body = models.TextField(blank=True, null=True, default='text')
    image = models.ImageField(upload_to='post_pics', blank=True)
    likes = models.ManyToManyField(User, blank=True, related_name='post_likes')

I am not populating likes field anywhere, my admin screenshot is below.Why my likes field is getting populated with all the users created?

admin screenshot

admin.py

from django.contrib import admin
from feed.models import UserPost
# Register your models here.
admin.site.register(UserPost)

Edit 1: Adding a screenshot for Rishabh's answer.

trying to add user from left as suggested by him


Solution

  • You are using ManyToManyField of Django which will show you all records of User table left side and you can add them by double clicking on them or by clicking '+' sign, so your likes field is not prepopulated at all it is just showing User table data.