admin page does not show list_display fields in django? - django

my admin page does not show the fields listed in admin.py.
Here is the admin.py
from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from customRegistration.models import ExUserProfile
admin.site.unregister(User)
class UserProfileInline(admin.StackedInline):
model = ExUserProfile
class UserProfileAdmin(UserAdmin):
inlines = [ UserProfileInline, ]
list_display = ('username', 'email','dateofBirth')
admin.site.register(User, UserProfileAdmin)
I do not see the username and dateofBirth field on my admin page.
models.py:
from django.db import models
from registration.models import User
from registration.signals import user_registered
class ExUserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
ishuman = models.BooleanField(required=True)
dateofBirth=models.DateField(required=True)
def __unicode__(self):
return self.user
def user_registered_callback(sender, user, request, **kwargs):
profile = ExUserProfile(user = user)
profile.ishuman = bool(request.POST["ishuman"])
profile.dateofBirth=request.POST["dateofBirth"]
print request.POST["dateofBirth"]
profile.save()
user_registered.connect(user_registered_callback)

Related

Django email address not showing in admin

I'm trying to create a simple email and name collector , everything looks fine but I can only see the name option in the admin site the email option is not there
admin page model
Here is my code
Forms.py
from django import forms
from sonus import models
from django import forms
class NameForm(forms.Form):
your_name = forms.CharField(label="Your Name",max_length=20)
your_email = forms.EmailField(label="Email",max_length=100)
Here Goes my Views.py
def get_name(request):
person = Person()
if request.method=="POST":
form = NameForm(request.POST)
if form.is_valid():
person.name = form.cleaned_data['your_name']
person.email = form.cleaned_data['your_email']
person.save()
return HttpResponseRedirect(reverse('index'))
else:
form = NameForm()
return render(request,'form.html',{ 'form': form })
My Admin.py
from django.contrib import admin
from django.contrib.admin.decorators import display
from django.contrib.auth.admin import UserAdmin
from .models import Details, Person,list
from sonus import models
# Register your models here.
admin.site.register(Person)
Here is the model
class Person(models.Model):
name = TextField(max_length=20)
email = EmailField(max_length=100)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('person',kwargs={'pk': self.pk})
In your admin.py change your code to that :
class PersonAdmin(admin.ModelAdmin):
list_display = ('name', 'email',)
admin.site.register(Person, PersonAdmin)

how to save CustomUser model to another model data

here is my code at users/admin.py,
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from store.models import Customer
from .forms import CustomUserCreationForm, CustomUserChangeForm
CustomUser = get_user_model()
class CustomUserAdmin(UserAdmin):
add_form = CustomUserCreationForm
form = CustomUserChangeForm
model = CustomUser
list_display = ['email', 'username',]
admin.site.register(CustomUser, CustomUserAdmin)
Also users/forms.py code is,
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
class CustomUserCreationForm(UserCreationForm):
class Meta:
model = get_user_model()
fields = ('email', 'username',)
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = get_user_model()
fields = ('email', 'username',)
I have table on store/models.py ,
class Customer(models.Model):
user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, null=True, blank=True,unique=True)
#name = models.CharField(max_length=200, null=True)
email = models.EmailField(max_length=200, null=True)
def __str__(self):
return self.user.username
Now can anyone please suggest me how to create a customer instances in user/admins.py or how to save data to customer table.

Exception Value:NOT NULL constraint failed: auth_user.username

I am creating a project to register and view profile using Django.
The problem is when I am trying to register a new user I am getting some errors
NOT NULL constraint failed: auth_user.username
Here is my form.py and view.py file:
form.py
from django.contrib.auth.models import User
from django import forms
from django.contrib.auth.forms import UserCreationForm,UserChangeForm
class FileDataForm(forms.Form):
name = forms.CharField(max_length=50)
file = forms.FileField()
image = forms.ImageField()
class userregister(UserCreationForm):
first_name = forms.CharField(max_length=50, required = False ,help_text ='optional')
last_name = forms.CharField(max_length=50, required = False ,help_text ='optional')
email = forms.EmailField(max_length=254, help_text='Required. Inform a valid email address.')
class Meta:
model = User
fields = ('first_name', 'last_name', 'email', 'password1', 'password2', )
class editprofileform(UserChangeForm):
class Meta:
model = User
fields = ('first_name', 'last_name', 'email','password')
View.py:
from django.shortcuts import render
from django.http import HttpResponse
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render,redirect,render_to_response
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import View
from .models import FileData
from .forms import FileDataForm
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from .forms import userregister,editprofileform
from django.contrib.auth.forms import UserChangeForm , PasswordChangeForm
from django.contrib.auth import update_session_auth_hash
# Create your views here.
#login_required
def home(request):
return HttpResponse('<h1>Welcome to your first page<h1>')
def registration(request):
print request
print 'request'
if request.method == 'POST':
#save the user data
form = userregister(request.POST)
print form.errors
print 'here'
if form.is_valid():
print 'i am here'
form.save()
return render(request , 'registration/success.html' ,{'form' : form,} )
else:
form = userregister()
return render(request , 'registration/register.html' ,{'form' : form,} )
else:
form = userregister()
return render(request , 'registration/register.html' , {'form': form,})
models.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
# Create your models here.
class UserProfile(models.Model):
user = models.OneToOneField(User)
description = models.CharField(max_length = 100,default='')
city = models.CharField(max_length =30)
mobile = models.IntegerField(default=0)
def create_profile(sender , **kwargs):
if kwargs['created']:
user_profile = UserProfile.objects.create(user = kwargs['instance'])
post_save.connect(create_profile , sender=User)
error:
Exception Value:
NOT NULL constraint failed: auth_user.username
In your form.py file, try inserting 'username' as the first element in the 'fields' list. It looks like you're not using that in the form so when you submit, it's leaving the username null.

Using CustomUser as author giving problem with database?

I am a little stuck, i am using CustomUser in settings (AUTH_USER_MODEL = 'users.CustomUser') and i want to use the username as author in comments.
class Comment(models.Model):
post = models.ForeignKey('blog.Post', on_delete=models.CASCADE, related_name='comments')
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True)
text = models.TextField()
when i run the migrations, it gives me this error
return self.cursor.execute(sql, params) django.db.utils.DataError:
invalid input syntax for integer: "Alandivar"
Alandivar being my superuser username in devmode,
so how can i make it so when a customer put a comment in (requires login), the username is automatically used as author
regards
customuser form
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser
from django import forms
class CustomUserCreationForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = CustomUser
fields = ('username', 'email')
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = CustomUser
fields = UserChangeForm.Meta.fields
customuser model
from django.contrib.auth.models import AbstractUser, UserManager
from django.db import models
class CustomUserManager(UserManager):
pass
class CustomUser(AbstractUser):
objects = CustomUserManager()
customuser view
from django.urls import reverse_lazy
from django.views import generic
from .forms import CustomUserCreationForm
class SignUp(generic.CreateView):
form_class = CustomUserCreationForm
success_url = reverse_lazy('login')
template_name = 'signup.html'

Extend the user model (avatar)?

I want to extend my user model and add avatar(image) field.
I created new app - accounts and inside the models.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
from django.conf import settings
from django.contrib.auth.models import User
def download_location_of_usrpic(instance, filename):
return "%s/%s" %(instance.id, filename)
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
image = models.ImageField(
upload_to=download_location_of_usrpic,
null=True,
blank=True,
height_field="height_field",
width_field="width_field"
)
height_field = models.IntegerField(default=0)
width_field = models.IntegerField(default=0)
My admin.py
from accounts.models import Profile
admin.site.register(Profile)
Made makemigrations and migrate
I also have a GCBV
#method_decorator(login_required, name='dispatch')
class UserUpdateView(UpdateView):
model = User
fields = ('first_name', 'last_name', 'email', 'image', )
template_name = 'my_account.html'
success_url = reverse_lazy('my_account')
def get_object(self):
return self.request.user
But I dont see this field in admin and got the mistake in account
Exception Value:
Unknown field(s) (image) specified for User
What did I miss?