【问题标题】:Wagtail Django admin model multiple images from orderableWagtail Django 管理员从可订购的多个图像中建模
【发布时间】:2020-07-05 23:00:53
【问题描述】:

我有一个位置模型:

class Location(models.Model):

    name = models.CharField(max_length=30, null=True, blank=False)

    image = models.ForeignKey(
        'wagtailimages.Image',
        null=True,
        blank=False,
        on_delete=models.SET_NULL,
        related_name="+",
    )

    address = models.CharField(max_length=60, null=True, blank=False)
    city = models.CharField(max_length=25)
    state = models.CharField(
        max_length=2, choices=misc_options.STATES, null=True, blank=False)
    zipcode = models.CharField(max_length=5, null=True, blank=False)

    lat = models.CharField(max_length=50, null=True, blank=False)
    lon = models.CharField(max_length=50, null=True, blank=False)

    phone = models.CharField(max_length=10, null=True, blank=False)
    email = models.CharField(max_length=40, null=True, blank=False)
    places_id = models.CharField(max_length=100, null=True, blank=True)
    facebook_url = models.CharField(max_length=100, null=True, blank=True)
    google_url = models.CharField(max_length=100, null=True, blank=True)

    entity = models.CharField(max_length=10, null=True, blank=True)
    truck_url = models.CharField(max_length=100, null=True, blank=True)
    trailer_url = models.CharField(max_length=100, null=True, blank=True)
    supplies_url = models.CharField(max_length=100, null=True, blank=True)
    features = models.ManyToManyField(Feature)

    panels = [
        MultiFieldPanel([
            FieldPanel("name"),
            InlinePanel('location_images'),
        ], heading="Store Info"),
        MultiFieldPanel([
            FieldPanel('city'),
            FieldPanel('state'),
            FieldPanel('zipcode'),
            FieldPanel('lat'),
            FieldPanel('lon'),
        ], heading="Location Info"),
        MultiFieldPanel([
            FieldPanel('phone'),
            FieldPanel('email'),
            FieldPanel('facebook_url'),
            FieldPanel('google_url'),
        ], heading="Contact Info"),
        MultiFieldPanel([
            FieldPanel('entity'),
            FieldPanel('truck_url'),
            FieldPanel('trailer_url'),
            FieldPanel('supplies_url'),
        ], heading="Web Self Storage Info"),
        MultiFieldPanel([
            FieldPanel('places_id'),
        ], heading="Google Places Info"),
        MultiFieldPanel([
            FieldPanel('features'),
        ], heading="Features Info"),
    ]

    class Meta:  # noqa
        ordering = ['name']
        verbose_name = "Location Detail"
        verbose_name_plural = "Location Details"

    def __str__(self):
        return self.name

这是一个管理模型。我需要将多个图像附加到此模型中的每个对象。我使用带有“功能”的外键字段来执行此操作,但这使得每个位置的所有功能都可用,并且必须从单独的界面编辑这些功能。我不希望图像出现这种情况。

我有一个图像可订购模型:

class LocationImages(Orderable):
    page = ParentalKey("location.LocationPage", related_name="location_images")
    location_image = models.ForeignKey(
        'wagtailimages.Image',
        null=True,
        blank=False,
        on_delete=models.SET_NULL,
        related_name="+",
    )

    panels = [
        ImageChooserPanel("location_image"),
    ]

但我被卡住了,因为 ParentalKey 不应该指向页面,它应该指向 Location 模型,如果我没记错的话。我认为多对一关系是最好的路线,但是当我在 modelcluster.fields 上运行 dir() 时,我看不到该选项。我在 modelcluster 上找不到任何文档。

最好的方法是什么?

【问题讨论】:

    标签: django wagtail


    【解决方案1】:

    事实证明,ParentalKey 非常聪明。它将模型名称而不是页面作为绑定对象。我还将我的 Location 模型更改为 ClusterableModel。已迁移,并且可以正常工作。

    class Location(ClusterableModel, models.Model):
    
        name = models.CharField(max_length=30, null=True, blank=False)
        ...
    

    class LocationImages(Orderable):
        page = ParentalKey(Location, related_name="location_images")
        ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-27
      • 2021-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-22
      相关资源
      最近更新 更多