简体   繁体   English

Django中manytomany字段自定义显示对象

[英]Manytomany field custom display objects in Django

I'm developing a site where I can manually add photographers to the Django adminpanel and then upload the images taken by them.我正在开发一个网站,我可以在其中手动将摄影师添加到 Django 管理面板,然后上传他们拍摄的图像。

I used the default User system in Django and added a field here that shows this user is a photographer or not:我在 Django 中使用了默认的用户系统,并在此处添加了一个字段来显示该用户是否是摄影师:

from django.contrib.auth.models import AbstractUser

# Create your models here.
class User(AbstractUser):
    is_photographer = models.BooleanField(default=True)

Also, in the model that I need to put the images, I used ManyToMany field for photographers (because these photos may have been taken by several people):此外,在我需要放置图像的 model 中,我为摄影师使用了 ManyToMany 字段(因为这些照片可能已被多个人拍摄):

from registration import User

class Photo(models.Model):
    ...
    photographers = models.ManyToManyField(User)

The problem is that when adding a new image to the admin panel, users who are not photographers and the is_photographer is False, is also displayed in the Photographers.问题是,当向管理面板添加新图像时,不是摄影师的用户并且is_photographer为 False,也会显示在摄影师中。 I want only users who are photographers to be displayed and not normal users.我只想显示摄影师用户,而不是普通用户。

In your admin.py file do this:在您的admin.py文件中执行此操作:

class PhotoAdmin(admin.ModelAdmin):
    def formfield_for_manytomany(self, db_field, request, **kwargs):
        kwargs['queryset'] = Users.objects.filter(is_photographer=True)
        return super().formfield_for_manytomany(db_field, request, **kwargs)

admin.site.register(Photo, PhotoAdmin)

You can work with the limit_choices_to=… [Django-doc] :您可以使用limit_choices_to=… [Django-doc]

from django.conf import settings

class Photo(models.Model):
    # …
    photographers = models.ManyToManyField(
        setting.AUTH_USER_MODEL,
        limit_choices_to={'is_photographer': True}
    )

Note : It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly.注意:通常最好使用settings.AUTH_USER_MODEL [Django-doc]来引用用户 model,而不是直接使用User model [Django-doc] For more information you can see the referencing the User model section of the documentation .有关详细信息,您可以参阅文档中引用User model的部分

You can use Django formfield .您可以使用Django 表单域 Django has placed these formfields for different types of fields. Django 为不同类型的字段放置了这些表单域。 To use formfields in ManyToMany relationships, you can use formfield_for_manytomany in the model admin_class.要在多对多关系中使用表单域,您可以在 model admin_class 中使用formfield_for_manytomany

Try putting this function in the Photo model admin class in the admin.py file of the application where the Photo model is located, and if you don't have a class admin, create one like this:试试把这个function放在Photo model admin class Photo model所在应用程序的admin.py文件中,如果你没有class admin,就这样创建一个:

from .models import Photo
from django.contrib import admin
from registration.models import User


class PhotoAdmin(admin.ModelAdmin):
    def formfield_for_manytomany(self, db_field, request, **kwargs):
        kwargs["queryset"] = User.objects.filter(is_photographer=True)
        return super(PhotoAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)

admin.site.register(Photo, PhotoAdmin)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM