Redefining class attributes in derived model classes, Django - django

I want to redefine class attribute in derived class. The email attribute is already exists in parent class User, but it's have no unique=True property as I need. I know that Django does not support this feature directly. So I did that:
class CustomUser(User):
"""The inherited User class. Email field are redefined to add the warning
about uniqueness is required. UserManager as the default manager so that
the standard methods are available."""
def __init__(self, *args, **kwargs):
super(CustomUser, self).__init__(*args, **kwargs)
email = models.EmailField(unique=True)
email.contribute_to_class(u'email', self)
objects = UserManager()
Traceback:
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/contrib/admin/options.py" in wrapper
307. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/utils/decorators.py" in _wrapped_view
93. response = view_func(request, *args, **kwargs)
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
79. response = view_func(request, *args, **kwargs)
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/contrib/admin/sites.py" in inner
196. return self.login(request)
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
79. response = view_func(request, *args, **kwargs)
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/contrib/admin/sites.py" in login
331. return login(request, **defaults)
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/utils/decorators.py" in _wrapped_view
93. response = view_func(request, *args, **kwargs)
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
79. response = view_func(request, *args, **kwargs)
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/contrib/auth/views.py" in login
35. if form.is_valid():
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/forms/forms.py" in is_valid
121. return self.is_bound and not bool(self.errors)
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/forms/forms.py" in _get_errors
112. self.full_clean()
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/forms/forms.py" in full_clean
268. self._clean_form()
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/forms/forms.py" in _clean_form
296. self.cleaned_data = self.clean()
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/contrib/admin/forms.py" in clean
26. self.user_cache = authenticate(username=username, password=password)
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/contrib/auth/__init__.py" in authenticate
55. user = backend.authenticate(**credentials)
File "/home/i159/workspace/students/backends.py" in authenticate
15. print self.user_class()
File "/home/i159/workspace/students/stdapp/models.py" in __init__
13. email.contribute_to_class(u'email', self)
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/db/models/fields/__init__.py" in contribute_to_class
235. self.set_attributes_from_name(name)
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/db/models/fields/__init__.py" in set_attributes_from_name
232. self.verbose_name = name.replace('_', ' ')
Exception Type: AttributeError at /admin/stdapp/customuser/add/
Exception Value: 'CustomUser' object has no attribute 'replace'
What can I do to fix it or how to set unique to True in email field? What the cause of this exception?
Edit:
Another way to do this.
First variation:
class CustomUser(User):
def __init__(self, *args, **kwargs):
setattr(self._meta.fields[4], 'unique', True)
Second:
class CustomUser(User):
def __init__(self, *args, **kwargs):
self._meta.fields[4].unique=True
Both are have the same error: can't set attribute.

Related

Creating object with generics CreateApiView 'request' required

This is my serializer and Viewset, i want to create an instance of Like if someone likes a product (Post request 'products/product:id/like'). But i get an error that 'request' is required.
class LikeSerializer(serializers.ModelSerializer):
user = serializers.PrimaryKeyRelatedField(read_only=True, default=serializers.CurrentUserDefault())
product = serializers.PrimaryKeyRelatedField(read_only=True)
class Meta:
model = Like
fields = ('user', 'product',)
class LikeProductApi(generics.CreateAPIView):
queryset = Like.objects.all()
serializer_class = LikeSerializer
def create(self, request, *args, **kwargs):
product_id = self.kwargs['pk']
product = ProductInStore.objects.get(id=int(product_id))
if Like.objects.filter(user=self.request.user, product=product).exists():
raise ValidationError(_("This user already likes this product"))
else:
return super().create(user=self.request.user, product_id=product_id, **kwargs)
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/handlers/base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/views/generic/base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/generics.py", line 190, in post
return self.create(request, *args, **kwargs)
File "/Users/jakubstrawa/programming/PythonKuba/api/ecommerce/views.py", line 124, in create
return super().create(user=self.request.user, product_id=product_id, **kwargs)
TypeError: create() missing 1 required positional argument: 'request'
Do you have any idea why?
You are making a super() call with the wrong parameters, it should be:
def create(self, request, *args, **kwargs):
product_id = self.kwargs['pk']
if Like.objects.filter(user=self.request.user, product_id=product_id).exists():
raise ValidationError(_("This user already likes this product"))
else:
return super().create(request, *args, **kwargs)
The issue is how you're calling create()
Try the below:
In the PythonKuba/api/ecommerce/views.py file
return super().create(request=self.request, product_id=product_id, **kwargs)
At the moment you are sending in the user, not the request

save() missing 1 required positional argument: 'request' in django

I create a model:
class Person(models.Model):
name = models.CharField(max_length=250)
slug = AutoSlugField(populate_from='name')
birth_date = models.DateField(null=True, blank=True)
blood_group = models.CharField(max_length=5)
present_address = models.CharField(max_length=250, blank=True)
permanent_address = models.CharField(max_length=250, blank=True)
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
related_name='member_persons')
forms.py:
class MemberForm(ModelForm):
class Meta:
model = Person
exclude = ('user',)
def save(self, request, commit=True):
person = super().save(commit=False)
if not person.pk:
person.user = get_user(request)
if commit:
person.save()
self.save_m2m()
return person
It worked fine for first person create. When same person again try to submit create form with different data it gives 'save() missing 1 required positional argument: 'request''. Full traceback:
Traceback:
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapper
67. return bound_func(*args, **kwargs)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/utils/decorators.py" in bound_func
63. return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapper
67. return bound_func(*args, **kwargs)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/utils/decorators.py" in bound_func
63. return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/edit.py" in post
217. return super(BaseCreateView, self).post(request, *args, **kwargs)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/edit.py" in post
183. return self.form_valid(form)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/edit.py" in form_valid
162. self.object = form.save()
Exception Type: TypeError at /person/create/
Exception Value: save() missing 1 required positional argument: 'request'
my views.py:
#require_authenticated_permission(
'member.add_person')
class PersonCreate(CreateView):
template_name = 'member/person_form.html'
model = Person
success_url = '/person/'
form_class = MemberForm
I want to show a message "You already submitted data" rather than getting this error. How can I show this message in the person creation form?
Any help will be appreciated.
Edit:
New error:
Traceback:
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapper
67. return bound_func(*args, **kwargs)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/utils/decorators.py" in bound_func
63. return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapper
67. return bound_func(*args, **kwargs)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/utils/decorators.py" in bound_func
63. return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/edit.py" in get
213. return super(BaseCreateView, self).get(request, *args, **kwargs)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/edit.py" in get
174. return self.render_to_response(self.get_context_data())
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/edit.py" in get_context_data
93. kwargs['form'] = self.get_form()
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/edit.py" in get_form
45. return form_class(**self.get_form_kwargs())
Exception Type: TypeError at /person/create/
Exception Value: __init__() got an unexpected keyword argument 'request'
save signatrue is invalid. there shouldn't be request in arguments. if you want to pass it to form use get_form
#require_authenticated_permission('member.add_person')
class PersonCreate(CreateView):
template_name = 'member/person_form.html'
model = Person
success_url = '/person/'
form_class = MemberForm
def get_form(self, form_class=None):
form = super().get_form(form_class)
form.request = self.request
return form
And then
class MemberForm(ModelForm):
class Meta:
model = Person
exclude = ('user',)
def save(self, commit=True):
person = super().save(commit=False)
if not person.pk:
person.user = get_user(self.request)
if commit:
person.save()
self.save_m2m()
return person
I send those parameters in this way
class MemberNew(generic.CreateView):
model = Member
form_class = MemberForm
template_name = "..."
def get_form(self, form_class=None):
return MemberForm(self.request, **self.get_form_kwargs())

'User' object has no attribute '__getitem__' error

I have the following model:
class StudentUsername(models.Model):
user = models.OneToOneField(User)
student = models.ForeignKey(Student)
When I try to add a user and student to this table using default Django Admin interface, I get the following error:
Exception Type: TypeError
Exception Value: 'User' object has no attribute '__getitem__'
Kindly help.
Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
112. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in wrapper
432. return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view
99. response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
52. response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\sites.py" in inner
198. return view(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapper
29. return bound_func(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view
99. response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in bound_func
25. return func(self, *args2, **kwargs2)
File "C:\Python27\lib\site-packages\django\db\transaction.py" in inner
371. return func(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in add_view
1133. self.log_addition(request, new_object)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in log_addition
600. action_flag=ADDITION
File "C:\Python27\lib\site-packages\django\contrib\admin\models.py" in log_action
19. e = self.model(None, None, user_id, content_type_id, smart_text(object_id), object_repr[:200], action_flag, change_message)
Exception Type: TypeError at /backoffice/students/studentusername/add/
Exception Value: 'User' object has no attribute '__getitem__'
It seems likely that you have defined a __unicode__ method on StudentUsername that is returning either a User or a Student object - that is, self.user or self.student - rather than actual unicode. So when Django tries to slice it, it gets this error.
Ensure that your unicode method actually returns unicode text.

Django 1.5.4 error_messages in ModelForm

I have a very simple ModelForm subclass:
class UserPrefsForm(forms.ModelForm):
class Meta:
model = User
fields = ['first_name','last_name','username']
When I try to save the form in my view (only the post function here):
def post(self, request):
userform = UserPrefsForm(request.POST)
context = self.get_context_data()
if userform.is_valid():
userform.save()
else:
context['errors'] = True
return render(request,self.template_name,context)
I got this error:
AttributeError at /prefs
'super' object has no attribute 'error_messages'
Traceback:
File "/home/tonjo/venv/tuned/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "/home/tonjo/venv/tuned/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/home/tonjo/venv/tuned/local/lib/python2.7/site-packages/braces/views.py" in dispatch
107. request, *args, **kwargs)
File "/home/tonjo/venv/tuned/local/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
86. return handler(request, *args, **kwargs)
File "/home/tonjo/prj/tuned_prj/tuned_django/graph/views.py" in post
147. if userform.is_valid():
File "/home/tonjo/venv/tuned/local/lib/python2.7/site-packages/django/forms/forms.py" in is_valid
126. return self.is_bound and not bool(self.errors)
File "/home/tonjo/venv/tuned/local/lib/python2.7/site-packages/django/forms/forms.py" in _get_errors
117. self.full_clean()
File "/home/tonjo/venv/tuned/local/lib/python2.7/site-packages/django/forms/forms.py" in full_clean
274. self._post_clean()
File "/home/tonjo/venv/tuned/local/lib/python2.7/site-packages/django/forms/models.py" in _post_clean
344. self.validate_unique()
File "/home/tonjo/venv/tuned/local/lib/python2.7/site-packages/django/forms/models.py" in validate_unique
353. self.instance.validate_unique(exclude=exclude)
File "/home/tonjo/venv/tuned/local/lib/python2.7/site-packages/django/db/models/base.py" in validate_unique
731. errors = self._perform_unique_checks(unique_checks)
File "/home/tonjo/venv/tuned/local/lib/python2.7/site-packages/django/db/models/base.py" in _perform_unique_checks
831. errors.setdefault(key, []).append(self.unique_error_message(model_class, unique_check))
File "/home/tonjo/venv/tuned/local/lib/python2.7/site-packages/django/db/models/base.py" in unique_error_message
882. return field.error_messages['unique'] % {
File "/home/tonjo/venv/tuned/src/neo4django/neo4django/utils.py" in __getattr__
303. return getattr(target, name)
I understand is the missing error_messages... Am I forced to define them?

Django Admin Issue - 'NoneType' object has no attribute 'user'

Writing a blog engine with Django 1.5 and using the Django Admin. Everything was fine, until I added a ManyToManyField to a model, and added that field to the ModelAdmin's fieldsets, then I started to get this mysterious error when trying to load the admin page:
'NoneType' object has no attribute 'user'
(Full stack trace further on below.) Why would the response object suddenly be None? If I remove the field from the fieldset, everything's fine again.
My models look something a bit like this (lots of fields removed):
class Tag(models.Model):
name = models.CharField(max_length=30)
class Post(models.Model):
title = models.CharField(max_length=300)
tags = models.ManyToManyField(Tag)
author = models.CharField(max_length=100)
And the ModelAdmin looks a bit like this:
class PostAdmin(admin.ModelAdmin):
# Order
fieldsets = (
('Content', {
'fields': ('title', 'tags')
}),
)
def formfield_for_dbfield(self, db_field, request, **kwargs):
# Pre-fill 'author' with logged in name
if db_field.name == "author":
kwargs['initial'] = "%s %s" % (request.user.first_name, request.user.last_name)
return db_field.formfield(**kwargs)
return super(PostAdmin, self).formfield_for_dbfield(db_field, **kwargs)
And it's when I remove 'tag's from the fieldsets that all is well again. It could be that I'm making some simple Django Admin mistake, I haven't used it much before, but the best I could find googling was some bug that was fixed three years ago, and I'm sure I'm running 1.5.1.
Here's the full stack trace:
Internal Server Error: /theadmin/blog/post/1/
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 115, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py", line 372, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py", line 91, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\views\decorators\cache.py", line 89, in _wrapped_view_
unc
response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\sites.py", line 202, in inner
return view(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py", line 25, in _wrapper
return bound_func(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py", line 91, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py", line 21, in bound_func
return func(self, *args2, **kwargs2)
File "C:\Python27\lib\site-packages\django\db\transaction.py", line 223, in inner
return func(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py", line 1081, in change_view
ModelForm = self.get_form(request, obj)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py", line 465, in get_form
return modelform_factory(self.model, **defaults)
File "C:\Python27\lib\site-packages\django\forms\models.py", line 424, in modelform_factory
return type(form)(class_name, (form,), form_class_attrs)
File "C:\Python27\lib\site-packages\django\forms\models.py", line 212, in __new__
opts.exclude, opts.widgets, formfield_callback)
File "C:\Python27\lib\site-packages\django\forms\models.py", line 170, in fields_for_model
formfield = formfield_callback(f, **kwargs)
File "E:\Dropbox\PassionateAbout\PassionateAboutJustice\blog\admin.py", line 35, in formfield_for
dbfield
return super(PostAdmin, self).formfield_for_dbfield(db_field, **kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py", line 125, in formfield_for_
bfield
related_modeladmin.has_add_permission(request))
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py", line 284, in has_add_permis
ion
return request.user.has_perm(opts.app_label + '.' + opts.get_add_permission())
AttributeError: 'NoneType' object has no attribute 'user'
Your swallowing the request. Here you take it:
def formfield_for_dbfield(self, db_field, request, **kwargs):
but then you don't pass it on:
return super(PostAdmin, self).formfield_for_dbfield(db_field, **kwargs)
However the original - https://github.com/django/django/blob/stable/1.5.x/django/contrib/admin/options.py#L88
is defined as:
def formfield_for_dbfield(self, db_field, **kwargs):
So you should keep the same signature when you override.
Please note: the original will do:
request = kwargs.pop("request", None)
If you want to access the request, don't do the same, because "pop()" will remove it from kwargs. Just access without deleting:
request = kwargs['request']
so your super call still passes the request through.
It only came to light with the M2M field because that needs the request to look up permissions of the current user (for the related model) whereas CharField doesn't really need the request, so doesn't mind that it's None. (which the above pop() call will do if request is not found)
End result:
def formfield_for_dbfield(self, db_field, **kwargs):
# Pre-fill 'author' with logged in name
if db_field.name == "author":
request = kwargs['request']
kwargs['initial'] = "%s %s" % (request.user.first_name, request.user.last_name)
return db_field.formfield(**kwargs)
return super(PostAdmin, self).formfield_for_dbfield(db_field, **kwargs)