Django - Logged in User Not Populating in admin.py - python

I’m trying to create a form to when the current logged in user makes a submission the user column in admin.py gets populated with the logged in user.
My problem:
The user column gets populated when a new user gets created using the CustomUserCreationForm however when the newly created user makes a form submission with the form listed below, the user column doesn’t get populated.
The Custom User Model that I'm trying to get the username from is located in from users.models import CustomUser so I’m not sure why this isn’t working.
How do I get the current logged in user to populate in admin.py in the users column with the form listed below?
Any help i gladly appreciated, thanks.
Code Below:
user_profile/models
from django.db import models
from django.urls import reverse
from django.contrib.auth.models import AbstractUser, UserManager
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.conf import settings
from users.forms import CustomUserCreationForm, CustomUserChangeForm
from users.models import CustomUser
class Listing (models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, null=True)
created = models.DateTimeField(auto_now_add=True, null=True)
updated = models.DateTimeField(auto_now=True)
name = models.CharField(max_length=100)
address = models.CharField(max_length=100)
zip_code = models.CharField(max_length=100)
mobile_number = models.CharField(max_length=100)
cc_number = models.CharField(max_length=100)
cc_expiration = models.CharField(max_length=100)
cc_cvv = models.CharField(max_length=100)
def create_profile(sender, **kwargs):
if kwargs['created']:
user_profile = Listing.objects.create(user=kwargs['instance'])
post_save.connect(create_profile, sender=CustomUser)
user_profile/admin.py
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from user_profile.forms import HomeForm
from users.forms import CustomUserCreationForm, CustomUserChangeForm
from user_profile.models import Listing
from users.models import CustomUser
# Register models here.
class UserProfileAdmin(admin.ModelAdmin):
list_display = ['name', 'address', 'zip_code', 'mobile_number', 'created', 'updated', 'user']
list_filter = ['name', 'zip_code', 'created', 'updated', 'user']
admin.site.register(Listing, UserProfileAdmin)
user_profile/views.py
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, redirect
from django.conf import settings
from .forms import HomeForm
from users.forms import CustomUserCreationForm, CustomUserChangeForm
from .models import Listing
from users.models import CustomUser
from django.urls import reverse_lazy
# add to your views
def change_view(request):
form = HomeForm(request.POST or None)
user_profile = Listing.objects.all
if form.is_valid():
form.save()
form = HomeForm()
context = {
'form': form, 'user_profile': user_profile
}
return render(request, "myaccount.html", context)
user_profile/forms.py
import os
from django import forms
from django.forms import ModelForm
from django.forms import widgets
from django.utils import six
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
from django.template.defaultfilters import filesizeformat
from avatar.conf import settings
from avatar.models import Avatar
from .models import Listing
class HomeForm(forms.ModelForm):
user = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'CVV', 'class': 'form-control'}))
username = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'CVV', 'class': 'form-control'}))
created = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'CVV', 'class': 'form-control'}))
name = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Full Name', 'class': 'form-control'}))
address = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Address', 'class': 'form-control'}))
zip_code = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Zipcode', 'class': 'form-control'}))
mobile_number = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Mobile Number', 'class': 'form-control'}))
cc_number = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Credit Card', 'class': 'form-control'}))
cc_expiration = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Expiration Date', 'class': 'form-control'}))
cc_cvv = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'CVV', 'class': 'form-control'}))
class Meta:
model = Listing
fields = '__all__'
settings.py
AUTH_USER_MODEL = 'users.CustomUser'

Try this
forms.py
import os
from django import forms
from django.forms import ModelForm
from django.forms import widgets
from django.utils import six
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
from django.template.defaultfilters import filesizeformat
from avatar.conf import settings
from avatar.models import Avatar
from .models import Listing
class HomeForm(forms.ModelForm):
class Meta:
model = Listing
fields = ('name', 'address', 'zip_code', 'mobile_number', 'cc_number', 'cc_number', 'cc_expiration', 'cc_cvv')
Add in models file
class ListingManager(models.Manager):
def save_from_object(self, request, obj):
obj.user = request.user
obj.save()
Add Update your Listing Model with objects as new manager
class Listing (models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, null=True)
created = models.DateTimeField(auto_now_add=True, null=True)
updated = models.DateTimeField(auto_now=True)
name = models.CharField(max_length=100)
address = models.CharField(max_length=100)
zip_code = models.CharField(max_length=100)
mobile_number = models.CharField(max_length=100)
cc_number = models.CharField(max_length=100)
cc_expiration = models.CharField(max_length=100)
cc_cvv = models.CharField(max_length=100)
objects = ListingManager()
This should work, Also I am not sure about your idea of keeping user as OneToOneField (This will not allow having multiple records with same user instance), I think you need to change it to ForeignKey. Refer What's the difference between django OneToOneField and ForeignKey?

Related

Django Admin doesn't recognise blank=True in model

When I try to edit a user (using a custom UserChangeForm) in the Django admin panel, validation insists that fields I have set blank=True in the model are required.
I don't know where to begin solving this; I had the same issue with the CustomUserCreationForm but reverted to using the default which works as expected (asks for username, password1 & password2, creates the user with blank display_name, bio and profile_picture fields).
models.py:
from django.db import models
from django.contrib.auth.models import AbstractUser
class CustomUser(AbstractUser):
display_name = models.CharField(max_length=30, blank=True, null=True)
bio = models.TextField(blank=True, null=True)
profile_picture = models.ImageField(upload_to='images/', blank=True, null=True)
def save(self, *args, **kwargs):
if not self.display_name:
self.display_name = self.username
super().save(*args, **kwargs)
def __str__(self):
return self.username
forms.py:
from django import forms
from django.contrib.auth.forms import UserChangeForm
from .models import CustomUser
class CustomUserChangeForm(UserChangeForm):
display_name = forms.CharField(label="display_name")
bio = forms.CharField(widget=forms.Textarea)
profile_picture = forms.ImageField(label="profile_picture")
class Meta():
model = CustomUser
fields = ("username", "email", "display_name", "bio", "profile_picture")
admin.py:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .forms import CustomUserChangeForm
from .models import CustomUser
class CustomUserAdmin(UserAdmin):
form = CustomUserChangeForm
fieldsets = (
(None,
{'fields': ('username', 'password', 'email', 'display_name', 'bio', 'profile_picture')}
),
)
model = CustomUser
list_display = ["username", "email",]
admin.site.register(CustomUser, CustomUserAdmin)
From the Django documentation:
By default, each Field class assumes the value is required, so if you
pass an empty value – either None or the empty string ("") – then
clean() will raise a ValidationError exception:
So you have to add required=False in your forms.py. For example:
display_name = forms.CharField(required=False, label="display_name")

Profile() got an unexpected keyword argument 'user'

Hi i working with Django .
I'm trying to turn my user into a profile with signals
When registering the user through a form
I get the following error :
TypeError at /Registro/ Profile() got an unexpected keyword argument 'user' and
the user is created in 'AUTHENTICATION AND AUTHORIZATION' (ADMIN), but not in profiles.
Models.py
from django.db import models
class Profile(models.Model):
id = models.AutoField(primary_key=True)
nombreUsuario = models.CharField('Nombre usuario : ', max_length=15, null = False, blank=False, unique=True)
email = models.EmailField('Email', null=False, blank=False, unique=True)
password = models.CharField('Contraseña', max_length=25, null=False, blank=False, default='')
#Unique sirve para validar si el usuario existe y sea unico el email y nombre de usuario.
nombres = models.CharField('Nombres', max_length=255, null= True, blank=True)
apellidos = models.CharField('Apellidos', max_length=255, null=True, blank=True)
imagen = models.ImageField(upload_to='img_perfil/',default='batman.png',null=True, blank=True)
fecha_union = models.DateField('Fecha de alta', auto_now = False, auto_now_add = True)
facebook = models.URLField('Facebook', null=True, blank=True)
instagram = models.URLField('Instagram', null=True, blank=True)
def __str__(self):
return f'Perfil de {self.nombreUsuario}'
class Meta:
verbose_name = "Perfil"
verbose_name_plural = "Perfiles"
views.py
from django.shortcuts import render, redirect
from django.http import HttpResponseRedirect
from django.views.generic.edit import FormView
from .models import Profile
from .forms import RegistrationForm
from django.contrib import messages
from django.contrib.auth.models import Group
from django.utils.decorators import method_decorator
def iniciarSesion(request):
return render(request,'social/inicio.html')
def registro(request):
if request.method == 'POST':
fm = RegistrationForm(request.POST)
if fm.is_valid():
user=fm.save()
username = fm.cleaned_data.get('username')
messages.success(request,'Registration Created Successfully')
redirect('feed')
else:
fm = RegistrationForm()
return render(request, 'social/registrarse.html',{'fm':fm})
def feed(request):
return render(request,'social/feed.html')
def profile(request):
return render(request,'social/profile.html')
forms.py
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from .models import Profile
class RegistrationForm(UserCreationForm):
class Meta:
model=User
fields=[
'username',
'email',
'first_name',
'last_name',
]
signals.py
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.contrib.auth.models import Group
from .models import Profile
def create_user_profile(sender, instance, created, **kwargs):
if created:
#group = Group.objects.get(name = 'profile')
#instance.groups.add(group)
Profile.objects.create(
user = instance,
name= instance.username,
)
Profile.objects.create(user=instance)
post_save.connect(create_user_profile, sender=User)
I need help with this code!
Well, what exactly did you expect? Your Profile model doesn't seem to have a fk to user and that's what you are trying to do (twice) in the signal.
Just add user fk to User model and create it once in the signal.

Remove/hiding username field in django admin edit user form

The frontend user signup form doesn't have username field as I am using email to login.Now I have access to django admin.
When I edit an user I get that username field to edit but I do not want that field to be editable or view-able at all.
admin.py --
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from .models import UserProfile
class ProfileInline(admin.StackedInline):
model = UserProfile
can_delete = False
verbose_name_plural = 'Profile'
fk_name = 'user'
class CustomUserAdmin(UserAdmin):
inlines = (ProfileInline, )
list_display = ('email', 'first_name', 'last_name', 'is_staff')
list_select_related = ('profile', )
exclude = ('username',)
def get_inline_instances(self, request, obj=None):
if not obj:
return list()
return super(CustomUserAdmin, self).get_inline_instances(request, obj)
admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)
forms.py --
from django import forms
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Div, Field
from ajax_select.fields import AutoCompleteSelectField, AutoCompleteField
from phonenumber_field.formfields import PhoneNumberField
from . import models
from captcha.fields import ReCaptchaField
class SignUpForm(forms.Form):
first_name = forms.CharField(max_length=30)
last_name = forms.CharField(max_length=30)
phone_number = PhoneNumberField(label=_("Phone (Please state your country code eg. +44)"))
organisation = forms.CharField(max_length=50)
email = forms.EmailField()
password1 = forms.CharField(max_length=20)
password2 = forms.CharField(max_length=20)
captcha = ReCaptchaField(attrs={'theme' : 'clean'})
models.py--
from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
from easy_thumbnails.fields import ThumbnailerImageField
from ciasroot.settings import THUMBNAILER_SIZES, UPLOAD_PATH
from ciasroot.constants import GENDERS, LANGUAGES
from ciasroot.util import HashedPk
from phonenumber_field.modelfields import PhoneNumberField
import math, decimal, datetime, os
class UserProfile(models.Model, HashedPk):
user = models.OneToOneField(User, unique=True, related_name ='profile')
job_title = models.CharField(max_length=128, blank=True, null=False, default="")
website = models.URLField(max_length=255, blank=True, null=True)
organisation = models.CharField(max_length=50, blank=True, null=True, default="")
phone_number = PhoneNumberField( blank=True, null=True)
The django version is 1.10 so I used exclude. But it throws an error--
KeyError: "Key 'username' not found in 'UserForm'. Choices are: date_joined, email, first_name, groups, is_active, is_staff, is_superuser, last_login, last_name, password, user_permissions."
Do I need to override the signup form because when adding a new user from admin I don't get the fields as frontend signup form(At some point I need to do that as well).
Any help is highly appreciated.
A discussion about this problem is here.
Briefly:
That's because the stock UserAdmin defines fieldsets in which 'username'
is a field, you need to modify the fieldsets, too.
For example:
class CustomUserAdmin(UserAdmin):
# ...
exclude = ('username',)
fieldsets = (
('Personal info', {'fields': ('full_name', 'email', 'password')}),
('Important dates', {'fields': ('last_login', 'date_joined')}),
('Permissions', {'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions')}),
)

django rest framework add user and create data in db

every one,I now using django rest framework(3.4) on my project(django 1.8+),I can create new user but I can not use new user to create data in db(I can do it in forms ok), however,I can create data in db by admin. I have to make the new user to create data in db,how can I do that?thanks for any one who reply.
models.py
class ProductsTbl(models.Model):
model_number = models.CharField(
max_length=255,
blank=True,
unique=True,
error_messages={
'unique': "這 model number 已經被註冊了 ."
}
)
name = models.CharField(max_length=255, blank=True, null=True)
material = models.CharField(max_length=255, blank=True, null=True)
color = models.CharField(max_length=255, blank=True, null=True)
feature = models.TextField(blank=True, null=True)
created = models.DateTimeField(editable=False)
modified = models.DateTimeField(auto_now=True)
release = models.DateTimeField(blank=True, null=True)
twtime = models.DateTimeField(blank=True, null=True)
hktime = models.DateTimeField(blank=True, null=True)
shtime = models.DateTimeField(blank=True, null=True)
jptime = models.DateTimeField(blank=True, null=True)
suggest = models.TextField(blank=True, null=True)
description = models.TextField(blank=True, null=True)
cataloggroup = models.ManyToManyField(CatalogGroup)
place = models.ManyToManyField(Place)
scale = models.ManyToManyField(Scale)
slug = models.SlugField(unique=True)
user = models.ForeignKey(User, blank=True, null=True)
useredit = models.CharField(max_length=32, blank=True, null=True)
def __unicode__(self):
return self.name
def save(self, *args, **kwargs):
''' On save, update timestamps '''
if not self.id:
self.created = timezone.now()
return super(ProductsTbl, self).save(*args, **kwargs)
api/serializers.py
from rest_framework import serializers
from ..models import *
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
UserModel = get_user_model()
class ProductsTblSerializer(serializers.ModelSerializer):
class Meta:
model = ProductsTbl
fields = ('model_number',
'created',
'name',
'release',
'twtime',
'hktime',
'shtime',
'jptime',
'feature',
'material',
'suggest',
'description',
'cataloggroup',
'place',
'scale',
'slug',
'user')
class UserSerializer(serializers.ModelSerializer):
password = serializers.CharField(write_only=True)
def create(self, validated_data):
user = UserModel.objects.create(
username=validated_data['username']
)
user.set_password(validated_data['password'])
user.save()
return user
class Meta:
model = UserModel
api/urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^productsTbls/$', views.ProductsTblListView.as_view(), name='productsTbls_list'),
url(r'^productsTbls/(?P<pk>\d+)/$', views.ProductsTblDetailView.as_view(), name='productsTbls_detail'),
url(r'^productsTbls/pdelete/(?P<id>[-\w]+)/$',views.api_delete_product,name='api_delete_p'),
url(r'^productsTbls/register/$', views.CreateUserView.as_view(), name='productsTbls_register'),
]
api/views.py
from rest_framework import generics
from ..models import *
from .serializers import ProductsTblSerializer
from django.contrib.auth.decorators import login_required
from django.http import Http404, HttpResponse
from django.shortcuts import render, redirect
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from django.views.decorators.csrf import csrf_exempt
from django.forms import modelformset_factory
from django.template.defaultfilters import slugify
from rest_framework import permissions
from rest_framework.generics import CreateAPIView
from django.contrib.auth import get_user_model
from .serializers import UserSerializer
class ProductsTblListView(generics.ListCreateAPIView):
queryset = ProductsTbl.objects.order_by('-created')
serializer_class = ProductsTblSerializer
class ProductsTblDetailView(generics.RetrieveUpdateDestroyAPIView):
queryset = ProductsTbl.objects.all()
serializer_class = ProductsTblSerializer
class CreateUserView(CreateAPIView):
model = get_user_model()
permission_classes = [
permissions.AllowAny # Or anon users can't register
]
serializer_class = UserSerializer
#csrf_exempt
#login_required
def api_delete_product(request, id):
# grab the image
dp = ProductsTbl.objects.get(id=id)
# security check
if dp.user != request.user:
raise Http404
# delete the image
dp.delete()
# refresh the edit page
return redirect('/api/productsTbls/')
settings.py
........
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
]
}
I changed the settings.py then it can work
settings.py
......
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.AllowAny',
#'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
]
}
I think from admin portal you need to provide permissions to user you have created for each method PUT,POST,GET or provide AllowAny permission(Which will give access to all your created user for any request). For more details refer this

register django textarea widget in admin

I want to register standard django textarea widget to admin. Now I'm a little confused about this, because in documentation I have ModelAdmin.formfield_overrides example, I don't want to override it, I just want to register it.
Is there some simple way of doing this registration or should I override it and make a custom widget out of it?
models.py:
from django.db import models
from django_countries.fields import CountryField
import datetime
# Create your models here.
class ContactForm(models.Model):
first_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=150)
email = models.EmailField(max_length=250)
mobile = models.CharField(max_length=32, blank=True)
timestamp = models.DateTimeField(default=datetime.datetime.now)
birthday = models.DateField(null=True)
move_in_date = models.DateField(null=True)
move_out_date = models.DateField(null=True)
country = CountryField()
def __unicode__(self):
"""TODO: Docstring for __unicode__.
:returns: TODO
"""
return self.email
class Meta:
ordering = ['-timestamp']
forms.py:
from django.forms import ModelForm, extras
from .models import ContactForm
from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout
from crispy_forms.bootstrap import TabHolder, Tab
from django_countries.widgets import CountrySelectWidget
import datetime
def get_move_date_field():
"""TODO: Docstring for get_move_date_field.
:returns: return a DateField suitable for move_in_date and move_out_date
"""
return forms.DateField(widget=extras.SelectDateWidget(), initial=datetime.date.today())
class ContactView(ModelForm):
"""TODO: Docstring for ContactView.
:returns: ContactView is a class in which we are returning about user information.
"""
birthday = forms.DateField(widget=extras.SelectDateWidget(years=range(2025, 1939, -1)))
move_in_date = get_move_date_field()
move_out_date = get_move_date_field()
message = forms.CharField(widget=forms.Textarea)
helper = FormHelper()
helper.form_tag = False
helper.layout = Layout(
TabHolder(
Tab(
'Basic Information',
'first_name',
'last_name',
'email',
'birthday',
'country',
'mobile'
),
Tab(
'Moving and additional Information',
'move_in_date',
'move_out_date',
'message',
)
)
)
class Meta:
fields = ['first_name', 'last_name', 'email', 'mobile',
'birthday', 'move_in_date', 'move_out_date',
'country', 'message']
model = ContactForm
widgets = {
'country': CountrySelectWidget()
}
admin.py:
from django.contrib import admin
from .models import ContactForm
# Register your models here.
class ContactFormAdmin(admin.ModelAdmin):
"""Docstring for ContactFormAdmin. """
class Meta:
model = ContactForm
admin.site.register(ContactForm, ContactFormAdmin)
I just forgot to register message inside `models.py'.
message = models.CharField(max_length=1000)
after makemigration and migrate I had textarea inside django admin.

Categories