Python DRF实现基于子字段的搜索功能

Python DRF实现基于子字段的搜索功能,python,django,django-rest-framework,Python,Django,Django Rest Framework,我需要一些建议。当前我的模型定义为 User = settings.AUTH_USER_MODEL class Profile(models.Model): #originally this is a one-to-one field but will cause an error in my nested serialization Userid= models.ForeignKey(User,on_delete=models.CASCADE,related_name='

我需要一些建议。当前我的模型定义为

User = settings.AUTH_USER_MODEL 

class Profile(models.Model):
    #originally this is a one-to-one field but will cause an error in my nested serialization 
    Userid= models.ForeignKey(User,on_delete=models.CASCADE,related_name='profile')
    X= models.CharField(max_length=50)

class Post(models.Model):
    Postid = models.AutoField(primary_key=True)
    Userid = models.ForeignKey(User,on_delete=models.CASCADE ,related_name='posting')
    Y = models.TextField()

User Model is the Django build in 'auth_User'. 
我的serialize.py是

class viewchildItemSeralizer(serializers.ModelSerializer):
    class Meta:
        model=Post
        exclude = ['Z'] #some other field in post that is not required


class viewchildProfileSeralizer(serializers.ModelSerializer):
    class Meta:
        model=Profile
        fields=['X']
    

class ViewItemSeralizer(serializers.ModelSerializer):
    posting= viewchildItemSeralizer(read_only=True ,many=True)
    profile = viewchildProfileSeralizer(read_only=True,many=True)
    class Meta:
        model=User
        fields=['username','email','posting','profile']
  
如果我的搜索基于概要文件中的X字段,那么代码可以正常工作(我可以用Json响应)

  • 实现另一个搜索函数,其中基于Post中Y字段上的“search_参数” 我只能返回特定的行值以及一些 用户(用户名、电子邮件)和个人资料(X)中的数据
我不认为queryset会是

User.objects.filter(post__Y=searcharg) 
# as that will just return all the Post Item for that User
#Unsure what would be the queryset if I am query from Post..  will search__related help ? 
另外,我很可能还需要更改或创建一个新的model.serializers,该model.serializers具有指向用户模型的自定义字段。如果是这样,我既不确定这是正确的方向,也不确定实现它的方法

欢迎提出任何建议或方向。。 谢谢