Django - Remove currently displayed image link In An Image Edit Form - django

I am using Django 2.2 for a project, I want to remove the currently displayed image link from the user update form as shown in the image below, how do I do this?
image
forms.py
from .models import Profile
class CreateUserForm(UserCreationForm):
class Meta:
model = get_user_model()
fields = ['username', 'email', 'password1', 'password2']
class UserUpdateForm(forms.ModelForm):
email = forms.EmailField()
class Meta:
model = User
fields = ['username', 'email']
help_texts = {
'username': None,
}
class ProfileUpdateForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['profile_pic']

You can try to change ImageField to FileInput using widgets. I use Django version 4.1.1 and it works for me.
# forms.py
class ProfileUpdateForm(forms.ModelForm):
profile_pic = forms.ImageField(widget=forms.FileInput)
class Meta:
...

Related

Is it possible to add more fields to admin/auth/user?

models.py
class UserProfile(User):
bio = models.TextField(blank=True, null=True)
pfp = models.ImageField(verbose_name='Profile Picture', blank=True, null=True, upload_to="images/profile")
forms.py
class UserRegisterForm(UserCreationForm):
email = forms.EmailField(widget=forms.EmailInput(attrs={"class":"form-control", "placeholder":"example#example.com"}))
first_name = forms.CharField(widget=forms.TextInput(attrs={"class":"form-control"}))
last_name = forms.CharField(widget=forms.TextInput(attrs={"class":"form-control"}))
pfp = forms.ImageField(required=False, widget=forms.FileInput(attrs={"class":"form-control"}))
bio = forms.CharField(required=False, widget=forms.Textarea(attrs={"class":"form-control", 'rows':5, "placeholder":"Write something about yourself..."}))
class Meta:
model = UserProfile
fields = ['first_name', 'last_name', 'username', 'email', "pfp", "bio"]
When creating the user, the two newly added fields (bio and pfp) are not being saved in admin/auth/user, so my question is, is it possible to add those fields to the admin users database?
views.py
class SignUpView(CreateView):
form_class = UserRegisterForm
template_name = "registration/signup.html"
success_url = reverse_lazy("login")
are not being saved in admin/auth/user
Indeed, these are safed on the UserProfile model.
You thus can make a ModelAdmin for this:
# app_name/admin.py
from django.contrib import admin
from app_name.models import UserProfile
#admin.register(UserProfile)
class AuthorAdmin(admin.ModelAdmin):
list_display = ('first_name', 'last_name', 'username', 'email', 'pfp', 'bio')
Then the details can be seen in the admin/app_name/userprofile section.
Create a custom user model by inheriting the AbstractUser and adding extra fields needed. After that, register that custom user model by assigning it to AUTH_USER_MODEL.
Check here for detailed implementation.

Wagtail profile avatar into ImageRenditionField serializer

I want to serialize the profile avatar from Wagtail admin with ImageRenditionField.
Basing on the answer from the question I tried this:
# models.py
class User(AbstractUser):
def get_avatar(self):
return self.wagtail_userprofile.avatar
# serializers.py
class UserSerializer(serializers.ModelSerializer):
avatar = ImageRenditionField('width-190', source='get_avatar')
class Meta:
model = User
fields = ['id', 'username', 'first_name', 'last_name', 'avatar']
I got this:
Django Version: 3.1.4
Exception Type: AttributeError
Exception Value:
'ImageFieldFile' object has no attribute 'get_rendition'
How do I throw user avatar into ImageRenditionField?
The avatar field is a regular Django ImageField. It does not have get_rendition, it does not have any Wagtail rendition logic.
Take a look at wagtail.users.models.UserProfile.avatar
class UserProfile(models.Model):
...
avatar = models.ImageField(
verbose_name=_('profile picture'),
upload_to=upload_avatar_to,
blank=True,
)
and wagtail.images.models.AbstractImage.file (this is the base class of Wagtail Image).
class AbstractImage(CollectionMember, index.Indexed, models.Model):
...
file = models.ImageField(
verbose_name=_('file'), upload_to=get_upload_to, width_field='width', height_field='height'
)
Notice that avatar field is a regular Django ImageField. The Wagtail image is a model that stores additional image information and handles filters, focal points and renditions.
You can NOT use the Wagtail renditions in combination with avatar field, because avatar is a Django ImageField and not a Wagtail Image.
There are various thumbnail packages that provide thumbnail/scale functionality for Django ImageFields. Don't adjust the UserProfile.avatar field itself (customising Wagtail). Search for packages that play nice with plain ImageFields.
This question and answer might interest you.
you can do this
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = "__all__
class PersonSerializer(serializers.ModelSerializer):
user = UserSerializer(read_only=True)
class Meta:
model = Person
fields = "__all__"
avatar = serializers.SerializerMethodField("get_avatar_display")
def get_avatar_display(self,info):
return info.wagtail_userprofile.avatar.url

create a user in Django 2.1 that is associated with an existing model

In my models.py file I have the following code ->
from django.db import models
class Blogger(models.Model):
username = models.CharField(max_length=20)
email = models.EmailField()
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
password = models.CharField(max_length=30, default='')
I want to associate the Blogger model with a User and create the User upon form submission. Here is the forms.py file ->
from django import forms
from blog.models import Blogger
class BloggerForm(models.ModelForm):
class Meta:
model = Blogger
fields = ['username', 'email', 'first_name', 'last_name', 'password']
And here is the views.py ->
class BlogView(FormView):
template_name = 'blogform.html'
form_class = BloggerForm
success_url = 'blog/'
How do I create a new user on the submission of this form ?
All fields in blogger already exists in User model, actually you don't need this Blogger model at all, just use the User model directly.
There's a couple ways you can do this but basically the general 2 answers are:
Toss/copy Django's user model and make your own (hard)
Extend the user model by making a new model, and relating it to the user model (easy)
I usually choose option #2 because then you don't have to reconfig the auth system. This is a good tutorial on how to do it: simpleisbetterthancomplex

Django Rest Framework object with relationship

How do I find an object with relationship and how to save an object in relationship with Django Rest Framework?
I looked in the documentation and found something similar to this
class UserSerializer(serializers.ModelSerializer):
profile = ProfileSerializer()
class Meta:
model = User
fields = ('username', 'email', 'first_name', 'last_name', 'profile')
class ProfileSerializer(serializers.ModelSerializer):
class Meta:
model = Profile
fields = ('nome_empresa', 'cnpj')
profile = ProfileSerializer()
NameError: name 'ProfileSerializer' is not defined
Python is not a compiled language. Everything is created/done at runtime.
At the following line:
profile = ProfileSerializer()
you are using the ProfileSerializer but at this point it is not created yet. It is created a little later. To fix this, you need to put it before the UserSerializer class:
class ProfileSerializer(serializers.ModelSerializer):
class Meta:
model = Profile
fields = ('nome_empresa', 'cnpj')
class UserSerializer(serializers.ModelSerializer):
profile = ProfileSerializer()
class Meta:
model = User
fields = ('username', 'email', 'first_name', 'last_name', 'profile')
Hope it helps!

rest serializer for nested class

i am very new to django , and started writing serializers and come with this issue
i have extended my user class from user_profile class
now i want to write the user_profile serializer to save data in user Profile and user
this is my user serializer
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'username', 'email','password','is_staff')
it working fine
this is my user_profile class
class User_Profile(models.Model):
class Meta:
db_table = 'user_profile'
user = models.OneToOneField(User, on_delete=models.CASCADE)
phoneNumber = models.CharField(max_length=10)
now i write this serializer for user profile
class UserProfileSerializer(serializers.HyperlinkedModelSerializer):
email = serializers.CharField(source='user.email')
username = serializers.CharField(source='user.username')
class Meta:
model = User_Profile
fields = ('url', 'phoneNumber','email','username')
but it does save the user and user profile data , and it gives error like this
Cannot assign "{u'username': u'test12', u'email': u'test#12.cm'}": "User_Profile.user" must be a "User" instance.
Please help!