step3

 1. adding user to the tweets

  update your models.py bu adding user column as

 from django.conf import settings

User=settings.AUTH_USER_MODEL
class Tweet(models.Model):
    user=models.ForeignKey(User,on_delete=models.CASCADE)
    content = models.TextField()
    image = models.ImageField(upload_to ='images/',blank=True,null=True)  #null is not required in db blank is not req in django

one user have many other things also but user will be the one


2. open shell and test for the filter 

    from tweets.models import Tweet

  obj=Tweet.objects.filter(content="hello") 

   obj   

 (output:<QuerySet [<Tweet: Tweet object (71)>]>)i.e in 71st obj content is hello

bj=Tweet.objects.filter(user=1) 

bj

 it returns all objects that has 1st user 

if you want to search the tweet object with username than

bj=Tweet.objects.filter(user__username="admin") [two __ are necessary ]

if you change user__username="admiN" i.e case Sensitive it wont give you any object since no any object is there with admiN

 to make case insensitive

bj=Tweet.objects.filter(user__username__iexact="Admin") [use iexact with 2 __ in front]


 basically to make case insensitive we use iexact

 to search content hello by typing Hello

bj=Tweet.objects.filter(content__iexact="Hello")



if you want to  add search bar to the admin section of that tweet and search admin name ant load tweet acc. to your search content than 

 upper logic comes to play

in admin.py 

# Register your models here.
class search(admin.ModelAdmin):
    search_fields=['content','user__username','user__email'] #this will add search bar to admin section and search content 
    #on the basis of the given input in it
    class Meta:
        model=Tweet

admin.site.register(Tweet,search)

it is case insensitive : iexact no need to place or you can do experiment(not full and final conclusion)

if you want to render username or any content inside models to RHS of each object than you can use 

list_display i.e

# Register your models here.
class search(admin.ModelAdmin):
    list_display=['__str__','user']  #this will render the content or given attribute to display on right hand side as reference
    #in this case it will display user of all objects in RHS: ['__str__','content'] : col-name=content and namein it =content of that item
    search_fields=['content','user__username','user__email'] #this will add search bar to admin section and search content 
    #on the basis of the given input in it
    class Meta:
        model=Tweet

admin.site.register(Tweet,search)


but attributes should be column name : for user--> user not user__username like previous search case

meaning of __str__

   in nepali tyesaiko : in upper case scenario: display the list of its own user



if you want to render the name of model  as its content name than you can use __str__ in models.py

User=settings.AUTH_USER_MODEL
class Tweet(models.Model):
    user=models.ForeignKey(User,on_delete=models.CASCADE)
    content = models.TextField()
    image = models.ImageField(upload_to ='images/',blank=True,null=True)  #null is not required in db blank is not req in django
    
    def __str__(self):
        return self.content









 



Comments