通过ManyToManyField重新组合查询集

时间:2019-05-31 22:56:00

标签: django django-models django-views

我正在使用django,我必须恢复名为Restaurante的模型的记录,该模型具有名称,地址,电话,城市等属性,并且具有food_type属性,该属性是ManyToManyFiel,使用我称为FoodType的模型,因为在餐厅中您可以提供多种类型的食物(例如墨西哥,意大利,素食主义者等)和某种类型的食物,因此可以在许多餐厅中提供它,并且我想恢复所有餐厅在X城市中,并按照以下食物类型将其分组:

  1. 素食食品
    • 堆肥机
    • 天然食品
    • GreenRestaurant
    • Resto Bar
  2. 意大利美食
    • Resto Bar
    • 派披萨
    • 堆肥机
  3. 墨西哥食物
    • Jalisquitos
    • 玛雅炸玉米饼
    • GreenRestaurant

我尝试使用标签{%regroup%}来完成此操作,但是它显示了预期的输出,我附上了模型代码:

class FoodType(models.Model):
    name = models.CharField(
        max_length = 150,
        blank = False,
        null = False,
        help_text = 'Enter the name of the type of food',
        verbose_name = 'Name'
    )
    description = models.CharField (
        max_length = 250,
        blank = False,
        null = False,
        help_text = 'Enter a brief description of this type of food',
        verbose_name = 'Description'
    )

class Restaurant(models.Model):
    name = models.CharField (
        max_length = 50,
        blank = False,
        null = False,
        help_text = 'Enter the name of the restaurant',
        verbose_name = 'Restaurant Name',
    )
    address = models.CharField (
        max_length = 200,
        blank = False,
        null = True,
        help_text = 'Enter the physical address of the restaurant',
        verbose_name = 'Addres'
    )
    telefono = PhoneNumberField(
        null = False,
        help_text = 'Enter the phone number of the restaurant in the format +999999999',
        unique = True,
        verbose_name = 'Phone Number'
    )
    city = models.ForeignKey(
        City,
        related_name = 'ubica_en',
        on_delete = models.DO_NOTHING,
        blank = False,
        null = True,
        help_text = 'Select the city to which this restaurant belongs',
        verbose_name = 'City',
    )
    administrator = models.ForeignKey(
        User,
        related_name = 'es_registrado_por',
        on_delete = models.CASCADE,
        blank = False,
        null = False,
        help_text = 'User who added this restaurant',
        verbose_name = 'Who registered it?'
    )
    food_type = models.ManyToManyField(
        FoodType,
        help_text = 'Select the type (s) of food served in this restaurant',
        related_name = 'restaurants',
        verbose_name = 'Type of food',
        blank = False
    )

我在代码中附加了标签{%regroup%}:

{% regroup restaurants by food_type as food_type_list %}
{% for food_type in food_type_list %}
  {{ food_type.grouper }}
  {% for restaurant in food_type.list %}
    <h1>{{ restaurant|upper }}</h1>
  {% endfor %}
{% endfor %}

希望您能提前帮助我,非常感谢。

1 个答案:

答案 0 :(得分:0)

最后,我通过以下方式解决了该问题:

foods = FoodType.objects.all()
group = {}
for food in foods:
    restaurant_key = '_'.join([str(restaurant.pk) for restaurant in comida.restaurant_set.all()])
    if not restaurant_key in group and restaurant_key! = '':
        group[restaurant_key] = {'restaurants': food.restaurant_set.all(), 'foods': []}
    if restaurant_key! = '':
        group[restaurant_key]['foods'].append(food)
context ['group'] = group