如何在Django中过滤具有很多字段的查询集?

时间:2019-04-28 12:19:19

标签: django django-models django-views

我正在创建一个可以显示用户属性的应用程序,但是我面临的问题是创建特定的属性类别。

例如,如果用户单击Condour Apartment,则仅应显示那些属性。我想知道我们如何在由该类别标题连接的那些属性上显示。

这是有关属性和类别的models.py

class Category(models.Model):
   title = models.CharField(max_length=20)

   def __str__(self):
    return self.title


class Property(models.Model):
   title = models.CharField(max_length = 210,default = 'None')

   STATUS_CHOICES = (
   ('R','Rent'),
   ('S','Sale'),
    )

   status = models.CharField(max_length = 210,choices =    STATUS_CHOICES,default = 'None')

    price = models.IntegerField()

    area = models.CharField(max_length = 210,default = 'None')

   ROOM_CHOICES = (
     ('1','1'),
     ('2','2'),
     ('3','3'),
     ('4','4'),
     ('MORE','More'),
    )

    rooms = models.CharField(
      max_length = 210, 
      choices = ROOM_CHOICES,
      default = 'None')
   BATHROOM_CHOICES = (
      ('1','1'),
      ('2','2'),
      ('3','3'),
      ('4','4'),
    )
    bathroom = models.CharField(max_length = 210, choices = BATHROOM_CHOICES, default = 'None')
    address = models.CharField(max_length = 210, default = 'None')
    state = models.CharField(max_length = 210, default = 'None')
    code = models.CharField(max_length = 210, default = 'None')
    images = models.ImageField(upload_to = 'images',)
    info = models.TextField(max_length = 1000, default = 'None')
    parking = models.BooleanField(default = False)
    air = models.BooleanField(default = False)
    swimming = models.BooleanField(default = False)
    laundry = models.BooleanField(default = False)
    dealer_name = models.CharField(max_length = 210, default = 'None')
    dealer_email = models.EmailField(max_length = 210, default = 'abc@gmail.com')
    dealer_number = models.CharField(max_length = 210, default = 'Not mentioned')
    user = models.ForeignKey(User, related_name = 'user')
    timpestamp = models.DateTimeField(auto_now_add = True)
    category = models.ManyToManyField(Category)


    def get_absolute_url(self,*args,**kwargs):
      return reverse('profile_details:property', kwargs={'pk':self.pk,})

    def __str__(self):
      return self.title

这是类别属性列表的我的views.py

def Properties_Category_List(request):
    category_count = get_category_count()

    most_recent = models.Property.objects.order_by('-timpestamp')[:2]

    # I want to filter the model by the category title it was connected with

    model = models.Property.objects.filter(category__title='Multi Family')

    paginator = Paginator(model,1)
    page_var = 'list'
    page = request.GET.get(page_var)

    try:
        paginated_queryset = paginator.page(page)
    except PageNotAnInteger:
        paginated_queryset = paginator.page(1)
    except EmptyPage:
        paginated_queryset = paginator.page(page)

    index = paginated_queryset.number - 1 
    max_index = len(paginator.page_range)
    start_index = index - 3 if index >= 3 else 0
    end_index = index + 3 if index <= max_index - 3 else max_index
    page_range = list(paginator.page_range)[start_index:end_index]


    context = {
        "property":paginated_queryset,
        'page_var':page_var,
        'page_range':page_range,
        'most_recent':most_recent,
        'category_count':category_count
        }

    template_name = 'properties/properties_category_list.html'  
    return render(request,template_name,context)

1 个答案:

答案 0 :(得分:0)

您应该能够做到

\d

编辑

在相当普通的水平上,类似这样的

category = Category.objects.get(title="Condour Apartment") properties = Property.objects.filter(category=category)

category.html

{% for category in categories %} <a href="{% url 'categories' category.id%}">{{ category.title }}</a> {% endfor %}

urls.py

path('category/<int:pk>/`, views.category_list, name="categories")

views.py