0

我是 Django 新手,当我尝试让管理员一次将多个图像添加到用户配置文件时遇到了问题。

我创建了一个模型配置文件,它允许用户拥有一个配置文件,并且可以在配置文件中拥有多张照片。

我想使用 user_id 将图像链接到配置文件并使用 user_id 进行过滤。但是,当我尝试将多张照片上传到个人资料时,它会显示错误。

models.py
    class Profile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL,
                on_delete=models.SET_NULL, null=True,
                )

    GENDER_CHOICES = (
        ('F', 'Female'),
        ('M', 'Male')
        )
    date_of_birth = models.DateField(blank=True, null=True)
    gender = models.CharField(max_length=1, 
        default='F',
        blank=False,
        choices=GENDER_CHOICES
        )       
    about_me = SizedTextField(
        size_class=2, 
        null=True,
        blank=True
        )

    date_created = models.DateTimeField(default=timezone.now)          

class Photos(models.Model):
    user = models.ForeignKey(
        Profile,
        to_field='user',
        null=True, 
        blank=True,
        on_delete=models.SET_NULL
        )
    photo = models.ImageField(
        upload_to='photos/%Y/%m/%d/',
        blank=True
        )

管理员.py

class PhotosInline(admin.TabularInline):
    model = Photos
    fk_name = 'user'#only one photo allowed to be uploaded, I want more than one


@admin.register(Profile)
class ProfileAdmin(admin.ModelAdmin):
    list_display = ['user', 'date_of_birth','gender', 'about_me']
    inlines = [PhotosInline,]

为什么它只允许上传一张照片?我必须做什么?

4

0 回答 0