简体   繁体   中英

Django rest-framework filter models by the name of other models connected with foreign key

class Genre(models.Model):
    name = models.CharField(unique=True,max_length=255)
    def __str__(self):
        return self.name
class BlogText(models.Model):
    mytext = models.TextField(null=True)
    genre = models.ForeignKey(Genre, on_delete=models.CASCADE,null=True,blank=True)
    pub_date = models.DateTimeField('date published')

Each BlogText has Genre.

Now I can filter BlogText by contains

mytext = filters.CharFilter(lookup_expr='contains')

then I want to filter BlogText models by blogtext.genre.name

I googled around but not found the reference other than CharFilter Contains. (even can't find IntFilter... I try to filter genre id directly....)

How can I make it??

指定field_name

genre = filters.CharFilter(lookup_expr='contains', )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM