使用if条件django将图像字段重置为默认图像

reset imagefield to default image using if condition django

我的个人资料表格有两部分:个人和非政府组织(表格的非政府组织部分有图像字段)。用户可以在配置文件类型之间切换。如果用户从非政府组织切换到个人,我会在我的函数中将与非政府组织相关的字段更新为 NA。但是,我需要将与 NGO 关联的图像字段重置为默认值并删除图像。

models.py

class Profile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
# on_deleting user, profile will also be deleted
image = models.ImageField(default="profilepic.jpg",upload_to="profile_pictures")
profile_type = models.CharField(max_length=20,choices=PROFILE_TYPES, default='individual')
dob = models.DateField(null=True)
bio = models.TextField(null=True)
anonymous = models.BooleanField(default=False)
ngo_name = models.CharField(max_length=200, blank=True)
ngo_address = models.CharField(max_length=200, blank=True)
ngo_phone = models.CharField(max_length=200, blank=True)
ngo_registration_num = models.CharField(max_length=200, blank=True)
ngo_registration_cert = models.ImageField(default="default_ngo.png",upload_to="ngo_documents")
ngo_status = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
    return f'{self.user.username} Profile'

forms.py

class ProfileForm(forms.ModelForm):
class Meta:
    model = Profile
    fields = ['image','profile_type','dob','bio','anonymous','profile_type','ngo_name','ngo_address','ngo_phone','ngo_registration_num','ngo_registration_cert','ngo_status']

views.py

@login_required
def profile_edit(request):
    if request.method == 'POST':
        u_form = UserUpdateForm(request.POST, instance=request.user)
        p_form = ProfileForm(request.POST, request.FILES, instance=request.user.profile)
        if u_form.is_valid() and p_form.is_valid():
            if p_form.cleaned_data.get('profile_type') == 'ngo':
                u_form.save()
                p_form.save()
                messages.success(request, f'your profile has been updated as NGO ')
                return redirect('users:profile')
            else:
                u_form.save()
                temp_p_form = p_form.save(commit=False)
                temp_p_form.ngo_name = 'NA'
                temp_p_form.ngo_address = 'NA'
                temp_p_form.ngo_phone = 'NA'
                temp_p_form.ngo_registration_num = 'NA'
                temp_p_form.ngo_phone = 'NA'
                temp_p_form.ngo_status = False
                temp_p_form.ngo_registration_cert = NEED TO SET THIS TO DEFAULT IMAGE AGAIN
                temp_p_form.save()
                messages.success(request, f'your profile has been updated as Individual ')
                return redirect('users:profile')

    else:
        u_form = UserUpdateForm(instance=request.user)
        p_form = ProfileForm()
    context = {
        'u_form' : u_form,
        'p_form' : p_form
    }
    return render(request, 'users/profile_edit.html',context)

在您的模型中:

image = models.ImageField(default="profilepic.jpg",upload_to="profile_pictures", blank=True, null=True)

并且在视图中:

temp_p_form.ngo_status = False
temp_p_form.image.delete()
temp_p_form.image = None