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
Related
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")
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.
Here is models.py file.
here Teacher has a foreign key relation with User.
from django.db import models
from django.contrib.auth.models import User
from django.db import models
from django.conf import settings
from django.utils.text import slugify
from django.urls import reverse
# Create your models here.
import misaka
from departments.models import Department
teacher_rank = [
("Lecturer", "Lecturer"),
("Assistant professor", "Assistant professor"),
("Associate professor", "Associate professor"),
("Professor", "Professor"),
("Professor emeritus", "Professor emeritus"),
]
class Teacher(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
department = models.ForeignKey(Department,blank=False, related_name="teachers", on_delete=models.CASCADE)
profile_pic = models.ImageField(upload_to='teachers/profile_pics', blank=True)
Teacher_ID = models.CharField(max_length=20, unique=True, blank=False)
portfolio_site = models.URLField(blank=True)
academic_rank = models.CharField(blank=False, max_length=100, choices=teacher_rank)
teacher_slug = models.SlugField(allow_unicode=True, unique=True)
def __str__(self):
return self.user.username
def save(self, *args, **kwargs):
self.teacher_slug = slugify(self.user.username)
super().save(*args, **kwargs)
def get_absolute_url(self):
return reverse("teachers:teacher_detail",
kwargs={"department_slug":self.department.department_slug,
"teacher_slug":self.teacher_slug})
class Meta:
ordering = ["Teacher_ID"]
unique_together = ["Teacher_ID", "department"]
Here the details of teacher model is viewed under User model in admin page of django.
admin.py file
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
# Register your models here.
from .models import Teacher
class TeacherInline(admin.StackedInline):
model = Teacher
can_delete = False
verbose_name_plural = "teacher"
fk_name = 'user'
class TeacherAdmin(UserAdmin):
inlines = (TeacherInline, )
def get_inline_instances(self, request, obj=None):
if not obj:
return list()
return super(TeacherAdmin, self).get_inline_instances(request, obj)
admin.site.unregister(User)
admin.site.register(User, TeacherAdmin)
As the code is implemented, teacher details is shown under User section in admin site.
What i want is to view user details under Teacher section
I am newbie in Django, and I can’t figure out how to get username through request. I’m working on vocabulary type site and I need that every entry user creates would have username.
Here is my models.py
from django.db import models
class EngDict(models.Model):
orig_word = models.CharField(max_length=500, null=False,blank=False, verbose_name='Слово')
translate = models.CharField(max_length=500,null=False,blank=False, verbose_name="Перевод")
remarks = models.TextField(null=True, blank=True, verbose_name="Примечания")
published_by = models.CharField(max_length=50, verbose_name="Добавлено")
published_date = models.DateTimeField(auto_now_add=True, db_index=True, verbose_name="Дата добавления")
category = models.ForeignKey('Category', null=True, blank=True, on_delete=models.PROTECT, verbose_name = 'Категория')
class Meta:
verbose_name = ("Перевод")
verbose_name_plural = "Англо-русский словарь"
ordering = ['-published_date']
This is views.py
from django.shortcuts import render
from django.template import loader
from .models import EngDict, Category
from django.views.generic.edit import CreateView
from .forms import EngDictForm
from django.urls import reverse_lazy
from django.views.generic import TemplateView, ListView, FormView
from django.db.models import Q
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import login
class EngDictCreateView(CreateView):
template_name = 'dict/create.html'
form_class = EngDictForm
success_url = reverse_lazy('index')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['categories'] = Category.objects.all()
return context
I see that I need some function like def user_name(request) , but I can’t understand where i should write it and what must be inside of it. I need that published_by variable was automatically filled in with user login
Added exclude in forms.py
from django.forms import ModelForm
from .models import EngDict
from django import forms
class EngDictForm (ModelForm):
def clean_orig_word(self):
data = self.cleaned_data['orig_word']
if EngDict.objects.filter(orig_word = data).count():
raise forms.ValidationError("ТАКОЕ СЛОВО УЖЕ ЕСТЬ!")
return data
class Meta:
model = EngDict
fields = ('orig_word', 'translate', 'remarks', 'published_by')
exclude =['published_by']
Please do not use a CharField for that. If later the user changes their username, then it refers to a user that no longer exists. To refer to another object, one uses a relation field like a ForeignKey [Django-doc], OneToOneField [Django-doc], or , ManyToManyField [Django-doc]. Here it looks like a ForeignKey is what you are looking for:
from django.conf import settings
class EngDict(models.Model):
orig_word = models.CharField(max_length=500, null=False,blank=False, verbose_name='Слово')
translate = models.CharField(max_length=500,null=False,blank=False, verbose_name='Перевод')
remarks = models.TextField(null=True, blank=True, verbose_name='Примечания')
published_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.PROTECT,
editable=False,
verbose_name='Добавлено'
)
published_date = models.DateTimeField(auto_now_add=True, db_index=True, verbose_name='Дата добавления')
category = models.ForeignKey('Category', null=True, blank=True, on_delete=models.PROTECT, verbose_name = 'Категория')
class Meta:
verbose_name = ('Перевод')
verbose_name_plural = 'Англо-русский словарь'
ordering = ['-published_date']
In the form you should exclude published_by and in the CreateView, you can then "patch" the object with:
from django.contrib.auth.mixins import LoginRequiredMixin
class EngDictCreateView(LoginRequiredMixin, CreateView):
template_name = 'dict/create.html'
form_class = EngDictForm
success_url = reverse_lazy('index')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['categories'] = Category.objects.all()
return context
def form_valid(self, form):
form.instance.published_by = self.request.user
return super().form_valid(form)
Note: You can limit views to a class-based view to authenticated users with the
LoginRequiredMixin mixin [Django-doc].
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?