Django MTMField:limit_choices_to = other_ForeignKeyField_on_same_model?

时间:2021-11-17 02:53:27

I've got a couple django models that look like this:

我有几个django模型看起来像这样:

from django.contrib.sites.models import Site

class Photo(models.Model):
    title = models.CharField(max_length=100)
    site = models.ForeignKey(Site)
    file = models.ImageField(upload_to=get_site_profile_path) 

    def __unicode__(self):
        return self.title


class Gallery(models.Model):    
    name = models.CharField(max_length=40)
    site = models.ForeignKey(Site)
    photos = models.ManyToManyField(Photo, limit_choices_to = {'site':name} )    

    def __unicode__(self):
        return self.name

I'm having all kinds of fun trying to get the limit_choices_to working on the Gallery model. I only want the Admin to show choices for photos that belong to the same site as this gallery. Is this possible?

我正在尝试使用limit_choices_来处理Gallery模型,这有很多乐趣。我只希望管理员显示与此图库属于同一网站的照片的选项。这可能吗?

3 个解决方案

#1


1  

I would delete site field on my Photo model and add a ForeignKey to Gallery. I would remove limit_choices_to from photos fields on Gallery model.

我会在我的照片模型上删除网站字段,并将一个ForeignKey添加到图库。我会从Gallery模型的照片字段中删除limit_choices_to。

Because you are using ForeignKeys to Sites, that means sites don't share galleries and photos. Therefore having those I mentioned above is already useless.

因为您使用ForeignKeys到站点,这意味着站点不共享画廊和照片。因此,我上面提到的那些已经没用了。

class Photo(models.Model):
    title = models.CharField(max_length=100)
    gallery = models.ForeignKey(Gallery, related_name='photos')
    file = models.ImageField(upload_to=get_site_profile_path) 

    def __unicode__(self):
        return self.title


class Gallery(models.Model):    
    name = models.CharField(max_length=40)
    site = models.ForeignKey(Site)

    def __unicode__(self):
        return self.name

Once you set the site on a gallery all its photos will inherit this property. And the site will be accessible as photo_instance.gallery.site:

在图库中设置网站后,其所有照片都将继承此属性。该网站可以通过photo_instance.gallery.site访问:

@property
def site(self):
    return self.gallery.site

This should work as if you had a site field. But I haven't tested it.

这应该像您有一个站点字段一样工作。但我没有测试过它。

Things change or course, if you decide that a gallery or a photo can appear in multiple sites.

如果您决定图库或照片可以出现在多个网站中,则事情会发生变化或变化。

#2


4  

Yes. You need to override the form that admin uses for the Gallery model, then limit the queryset of the photos field in that form:

是。您需要覆盖管理员用于Gallery模型的表单,然后限制该表单中的photos字段的查询集:

class GalleryAdminForm(django.forms.ModelForm):

    class Meta:
        model = Gallery

    def __init__(self, *args, **kwargs):
        super(GalleryAdminForm, self).__init__(*args, **kwargs)
        self.fields['segments'].queryset = Photo.objects.filter(site=self.instance.site)


class GalleryAdmin(django.contrib.admin.ModelAdmin):
    form = GalleryAdminForm

django.contrib.admin.site.register(Gallery, GalleryAdmin)

#3


0  

According to the docs, "limit_choices_to has no effect when used on a ManyToManyField with an intermediate table". By my reading, that means it has no effect at all, because ManyToManyFields use intermediate tables...

根据文档,“limit_choices_to在具有中间表的ManyToManyField上使用时无效”。通过我的阅读,这意味着它根本没有任何影响,因为ManyToManyFields使用中间表...

I haven't tried to make it work in the Admin site, but from your own views, you can create a form and override the queryset used to populate the list of choices:

我没有尝试在Admin站点中使其工作,但是从您自己的视图中,您可以创建一个表单并覆盖用于填充选项列表的查询集:

form.fields["photos"].queryset = request.user.photo_set.all()

#1


1  

I would delete site field on my Photo model and add a ForeignKey to Gallery. I would remove limit_choices_to from photos fields on Gallery model.

我会在我的照片模型上删除网站字段,并将一个ForeignKey添加到图库。我会从Gallery模型的照片字段中删除limit_choices_to。

Because you are using ForeignKeys to Sites, that means sites don't share galleries and photos. Therefore having those I mentioned above is already useless.

因为您使用ForeignKeys到站点,这意味着站点不共享画廊和照片。因此,我上面提到的那些已经没用了。

class Photo(models.Model):
    title = models.CharField(max_length=100)
    gallery = models.ForeignKey(Gallery, related_name='photos')
    file = models.ImageField(upload_to=get_site_profile_path) 

    def __unicode__(self):
        return self.title


class Gallery(models.Model):    
    name = models.CharField(max_length=40)
    site = models.ForeignKey(Site)

    def __unicode__(self):
        return self.name

Once you set the site on a gallery all its photos will inherit this property. And the site will be accessible as photo_instance.gallery.site:

在图库中设置网站后,其所有照片都将继承此属性。该网站可以通过photo_instance.gallery.site访问:

@property
def site(self):
    return self.gallery.site

This should work as if you had a site field. But I haven't tested it.

这应该像您有一个站点字段一样工作。但我没有测试过它。

Things change or course, if you decide that a gallery or a photo can appear in multiple sites.

如果您决定图库或照片可以出现在多个网站中,则事情会发生变化或变化。

#2


4  

Yes. You need to override the form that admin uses for the Gallery model, then limit the queryset of the photos field in that form:

是。您需要覆盖管理员用于Gallery模型的表单,然后限制该表单中的photos字段的查询集:

class GalleryAdminForm(django.forms.ModelForm):

    class Meta:
        model = Gallery

    def __init__(self, *args, **kwargs):
        super(GalleryAdminForm, self).__init__(*args, **kwargs)
        self.fields['segments'].queryset = Photo.objects.filter(site=self.instance.site)


class GalleryAdmin(django.contrib.admin.ModelAdmin):
    form = GalleryAdminForm

django.contrib.admin.site.register(Gallery, GalleryAdmin)

#3


0  

According to the docs, "limit_choices_to has no effect when used on a ManyToManyField with an intermediate table". By my reading, that means it has no effect at all, because ManyToManyFields use intermediate tables...

根据文档,“limit_choices_to在具有中间表的ManyToManyField上使用时无效”。通过我的阅读,这意味着它根本没有任何影响,因为ManyToManyFields使用中间表...

I haven't tried to make it work in the Admin site, but from your own views, you can create a form and override the queryset used to populate the list of choices:

我没有尝试在Admin站点中使其工作,但是从您自己的视图中,您可以创建一个表单并覆盖用于填充选项列表的查询集:

form.fields["photos"].queryset = request.user.photo_set.all()