我如何通过 SELECT * DRF 中的 LIKE 条件获取数据列表?
How i can get list of data by SELECT * LIKE condition in DRF?
如何请求 DRF,就像通过 LIKE 条件请求 SELECT 一样?现在它们像这样显示给我
但我想根据 user_id 和 user_members_id 列中的值进行输出。我有这个代码
models.py
from django.contrib.postgres.fields import ArrayField
from django.db import models
from django.contrib.auth.models import User
def user_directory_path(instance, filename):
# file will be uploaded to MEDIA_ROOT / user_<id>/<filename>
return 'user_{0}/{1}'.format(instance.company.id, filename)
# Create your models here.
class Company(models.Model):
name = models.CharField(max_length=40, blank=True)
user_id = models.ForeignKey(User, verbose_name='User', on_delete=models.CASCADE) #models.IntegerField(blank=True)
user_members_id = ArrayField(models.IntegerField(), blank=True)
date_created= models.DateTimeField(auto_now=True)
description = models.TextField(blank=True)
# ready = models.CharField(max_length=10, blank=True)
STATUSES = (
(1, 'Public'),
(2, 'Private'),
(3, 'Protected'),
)
status = models.IntegerField(verbose_name='Status', choices=STATUSES)
THEMES = (
(1, 'Finance'),
(2, 'IT'),
(3, 'Develop'),
(4, 'Building'),
)
theme = models.IntegerField(verbose_name='Theme', choices=THEMES)
icon = models.ImageField(upload_to = user_directory_path, blank=True)
def __str__(self):
return self.name
serializers.py
from rest_framework import serializers
from .models import Company
class CompanySerializer(serializers.ModelSerializer):
class Meta:
model = Company
fields = '__all__'
urls.py
from rest_framework import routers
from .api import CompanyViewSet
router = routers.DefaultRouter()
router.register('api/company', CompanyViewSet, 'company')
urlpatterns = router.urls
views.py
from django.shortcuts import render
# Create your views here.
api.py
from .models import Company
from rest_framework import viewsets, permissions
from .serializers import CompanySerializer
class CompanyViewSet(viewsets.ModelViewSet):
queryset = Company.objects.all()
permission_classes = [
permissions.AllowAny
]
serializer_class = CompanySerializer
如何通过指定列值获得条件结果。例如 user_id 并且在 user_members_id 中有并且如果可能的话,由 token
您可以使用DRF's SearchFilter
from rest_framework.filters import SearchFilter
class CompanyViewSet(viewsets.ModelViewSet):
queryset = Company.objects.all()
permission_classes = [
permissions.AllowAny
]
serializer_class = CompanySerializer
<b>filter_backends = (SearchFilter,)
search_fields = ['username', 'email']</b>
要在您的 API 中搜索,请使用 search
查询参数传递您的搜索关键字。
/path/to/end-point/<b>?search=russell</b>
如何请求 DRF,就像通过 LIKE 条件请求 SELECT 一样?现在它们像这样显示给我
models.py
from django.contrib.postgres.fields import ArrayField
from django.db import models
from django.contrib.auth.models import User
def user_directory_path(instance, filename):
# file will be uploaded to MEDIA_ROOT / user_<id>/<filename>
return 'user_{0}/{1}'.format(instance.company.id, filename)
# Create your models here.
class Company(models.Model):
name = models.CharField(max_length=40, blank=True)
user_id = models.ForeignKey(User, verbose_name='User', on_delete=models.CASCADE) #models.IntegerField(blank=True)
user_members_id = ArrayField(models.IntegerField(), blank=True)
date_created= models.DateTimeField(auto_now=True)
description = models.TextField(blank=True)
# ready = models.CharField(max_length=10, blank=True)
STATUSES = (
(1, 'Public'),
(2, 'Private'),
(3, 'Protected'),
)
status = models.IntegerField(verbose_name='Status', choices=STATUSES)
THEMES = (
(1, 'Finance'),
(2, 'IT'),
(3, 'Develop'),
(4, 'Building'),
)
theme = models.IntegerField(verbose_name='Theme', choices=THEMES)
icon = models.ImageField(upload_to = user_directory_path, blank=True)
def __str__(self):
return self.name
serializers.py
from rest_framework import serializers
from .models import Company
class CompanySerializer(serializers.ModelSerializer):
class Meta:
model = Company
fields = '__all__'
urls.py
from rest_framework import routers
from .api import CompanyViewSet
router = routers.DefaultRouter()
router.register('api/company', CompanyViewSet, 'company')
urlpatterns = router.urls
views.py
from django.shortcuts import render
# Create your views here.
api.py
from .models import Company
from rest_framework import viewsets, permissions
from .serializers import CompanySerializer
class CompanyViewSet(viewsets.ModelViewSet):
queryset = Company.objects.all()
permission_classes = [
permissions.AllowAny
]
serializer_class = CompanySerializer
如何通过指定列值获得条件结果。例如 user_id 并且在 user_members_id 中有并且如果可能的话,由 token
您可以使用DRF's SearchFilter
from rest_framework.filters import SearchFilter
class CompanyViewSet(viewsets.ModelViewSet):
queryset = Company.objects.all()
permission_classes = [
permissions.AllowAny
]
serializer_class = CompanySerializer
<b>filter_backends = (SearchFilter,)
search_fields = ['username', 'email']</b>
要在您的 API 中搜索,请使用 search
查询参数传递您的搜索关键字。
/path/to/end-point/<b>?search=russell</b>