The following error message appears after I submit the SignUpform:
'NoneType' object has no attribute '_inc_path'
This issue is related to the role choice field of my CustomUser model. The models function perfectly without the role field and all forms are displayed and saved correctly.
I suspect my choice field form does not pass/save correctly the input values to the CustomUser model.
Any input would be highly appreciated.
Models.py:
class CustomUser(AbstractUser):
display_name = models.CharField(verbose_name=("Display name"), max_length=30, help_text=("Will be shown e.g. when commenting"))
...
country = CountryField(blank=True, null=True)
...
role = models.CharField(choices = ROLES, max_length = 50, default = "regular_user",)
...
class Meta:
ordering = ['last_name']
def get_absolute_url(self):
return reverse('account_profile')
def __str__(self):
return f"{self.username}: {self.first_name} {self.last_name}" ```
forms.py:
class SignupForm(forms.Form):
first_name = forms.CharField(max_length=30, label=_("First name"))
last_name = forms.CharField(max_length=30, label=_("Last name"))
display_name = forms.CharField(max_length=30, label=_("Display name"), help_text=_("Will be shown e.g. when commenting."))
role = forms.ChoiceField(choices = ROLES, label="Role", initial='Regular_user', widget=forms.Select(), required=True)
def signup(self, request, user):
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.display_name = self.cleaned_data['display_name']
user.role = self.cleaned_data['role']
user.save()
users/create.html:
{% extends "wagtailusers/users/create.html" %}
{% block extra_fields %}
...
{% include "wagtailadmin/shared/field_as_li.html" with field=form.role %}
...
{% endblock extra_fields %}
settings.py:
AUTH_USER_MODEL = 'userauth.CustomUser'
WAGTAIL_USER_CREATION_FORM ='userauth.forms.WagtailUserCreationForm'
WAGTAIL_USER_EDIT_FORM = 'userauth.forms.WagtailUserEditForm'
WAGTAIL_USER_CUSTOM_FIELDS = ['display_name',... 'role', ...]
ACCOUNT_SIGNUP_FORM_CLASS = 'userauth.forms.SignupForm'
Error log:
2022-10-17 10:16:35,894: Internal Server Error: /accounts/signup/
Traceback (most recent call last):
File "/home/teki/.virtualenvs/bup/lib/python3.9/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/home/teki/.virtualenvs/bup/lib/python3.9/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/teki/.virtualenvs/bup/lib/python3.9/site-packages/django/views/generic/base.py", line 84, in view
return self.dispatch(request, *args, **kwargs)
File "/home/teki/.virtualenvs/bup/lib/python3.9/site-packages/django/utils/decorators.py", line 46, in _wrapper
return bound_method(*args, **kwargs)
File "/home/teki/.virtualenvs/bup/lib/python3.9/site-packages/allauth/decorators.py", line 20, in wrap
resp = function(request, *args, **kwargs)
File "/home/teki/.virtualenvs/bup/lib/python3.9/site-packages/django/utils/decorators.py", line 46, in _wrapper
return bound_method(*args, **kwargs)
File "/home/teki/.virtualenvs/bup/lib/python3.9/site-packages/django/views/decorators/debug.py", line 92, in sensitive_post_parameters_wrapper
return view(request, *args, **kwargs)
File "/home/teki/.virtualenvs/bup/lib/python3.9/site-packages/allauth/account/views.py", line 234, in dispatch
return super(SignupView, self).dispatch(request, *args, **kwargs)
File "/home/teki/.virtualenvs/bup/lib/python3.9/site-packages/allauth/account/views.py", line 77, in dispatch
response = super(RedirectAuthenticatedUserMixin, self).dispatch(
File "/home/teki/.virtualenvs/bup/lib/python3.9/site-packages/allauth/account/views.py", line 207, in dispatch
return super(CloseableSignupMixin, self).dispatch(request, *args, **kwargs)
File "/home/teki/.virtualenvs/bup/lib/python3.9/site-packages/django/views/generic/base.py", line 119, in dispatch
return handler(request, *args, **kwargs)
File "/home/teki/.virtualenvs/bup/lib/python3.9/site-packages/allauth/account/views.py", line 105, in post
response = self.form_valid(form)
File "/home/teki/.virtualenvs/bup/lib/python3.9/site-packages/allauth/account/views.py", line 252, in form_valid
return complete_signup(
File "/home/teki/.virtualenvs/bup/lib/python3.9/site-packages/allauth/account/utils.py", line 183, in complete_signup
signals.user_signed_up.send(
File "/home/teki/.virtualenvs/bup/lib/python3.9/site-packages/django/dispatch/dispatcher.py", line 176, in send
return [
File "/home/teki/.virtualenvs/bup/lib/python3.9/site-packages/django/dispatch/dispatcher.py", line 177, in <listcomp>
(receiver, receiver(signal=self, sender=sender, **named))
File "/home/teki/fommestyuta/users/models.py", line 38, in create_user_group_and_pages
home.add_child(instance=article_index_page)
File "/home/teki/.virtualenvs/bup/lib/python3.9/site-packages/treebeard/mp_tree.py", line 1083, in add_child
return MP_AddChildHandler(self, **kwargs).process()
File "/home/teki/.virtualenvs/bup/lib/python3.9/site-packages/treebeard/mp_tree.py", line 377, in process
newobj.path = self.node.get_last_child()._inc_path()
AttributeError: 'NoneType' object has no attribute '_inc_path'
create_user_group_and_pages:
from django.db import models
from django.contrib.auth.models import Group, Permission
from django.dispatch import receiver
from allauth.account.signals import user_signed_up
from wagtail.core.models import Page, GroupPagePermission, GroupCollectionPermission, Collection
from cms.models import ArticleIndexPage
#receiver(user_signed_up)
def create_user_group_and_pages(sender, **kwargs):
"""
When a new user signs up create a unique group and page for them.
Assign it the appropriate permission for admin, page and collection access.
"""
# Grab the new user
user = kwargs['user']
# Create a group object that matches their username
new_group, created = Group.objects.get_or_create(name=user.username)
# Add the new group to the database
user.groups.add(new_group)
# Create new permission to access the wagtail admin
access_admin = Permission.objects.get(codename='access_admin')
# Add the permission to the group
new_group.permissions.add(access_admin)
# Now start creating page access
# First find the homepage
home = Page.objects.get(slug='home').specific
# Create unique PersonIndexPage for the user
article_index_page = ArticleIndexPage(title=user.username)
# Add PersonIndexPage to homepage as a child
home.add_child(instance=article_index_page)
# Save new page as first revision
article_index_page.save_revision()
# Create new add GroupPagePermission
GroupPagePermission.objects.create(
group=new_group,
page=article_index_page,
permission_type='add'
)
# Create new GroupCollectionPermission for Profile Images collection
GroupCollectionPermission.objects.create(
group=new_group,
collection=Collection.objects.get(name='Images'),
permission=Permission.objects.get(codename='add_image')
)
It looks like the page tree data in your database has become inconsistent, causing it to fail when creating the new ArticleIndexPage. (Specifically, the record for the 'home' page shows that it has existing child pages, meaning that it has to look at those child page records to find the correct position for the new page - but when it tries retrieving those child pages, none are being returned.) This may have happened due to a previous error occurring part-way through creating or deleting a page.
To fix this, run: ./manage.py fixtree
Related
I am on django 4.0.4 and have a benign but annoying problem with the above error code.
Previous responses to similar questions don't help me.
When I backspace, (an incovenience a user may not think of) everything has actually gone well, the list and detail views are correctly updated.
The create form is however as it was as if it was not saved, and the view doesn't change to the detail view as suggested by the get_absolute_url, which may lead one to process it again.
The code from previous models is exactly the same where it matters. An example is the TaskCreate view which is giving the problem and the ObjectiveCreate view just before it which works as expected.
In the views, I find the user and his/her entity (even though there is one entity), and process that user as a supervisor1 and also automatically process the entity.
The error finds fault with the TaskCreate view's "return super().form_valid(form)" but not with that in the ObjectiveCreate view.
Here is the code for the two views
class ObjectiveCreate(LoginRequiredMixin, CreateView):
model = Objective
form_class = ObjectiveCreateForm
template_name = "internalcontrol/objective_create_form.html"
def form_valid(self, form):
user = self.request.user
profile = Profile.objects.get(user=user)
entity = profile.entity
new_profile = Profile.objects.get(user=user, entity=entity)
form.instance.supervisor1 = new_profile
form.instance.entity = entity
return super().form_valid(form)
def get_form_kwargs(self):
kwargs = super(ObjectiveCreate, self).get_form_kwargs()
kwargs["request"] = self.request
return kwargs
class TaskCreate(LoginRequiredMixin, CreateView):
model = Task
form_class = TaskCreateForm
template_name = "internalcontrol/task_create_form.html"
def form_valid(self, form):
user = self.request.user
profile = Profile.objects.get(user=user)
entity = profile.entity
new_profile = Profile.objects.get(user=user, entity=entity)
form.instance.supervisor1 = new_profile
form.instance.entity = entity
return super().form_valid(form)
def get_form_kwargs(self):
kwargs = super(TaskCreate, self).get_form_kwargs()
kwargs["request"] = self.request
return kwargs
The respective get_absolute_url for both models are:
def get_absolute_url(self):
return reverse(
"internalcontrol:objective_detail",
kwargs={"pk": self.pk},
)
and
def get_absolute_url(self):
return (
reverse("internalcontrol:task_detail", kwargs={"pk": self.pk}),
)
The traceback is as follows:
Internal Server Error: /task_create/
Traceback (most recent call last):
File "C:\Users\User\.virtualenvs\nguyena-QPBkFrkQ\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "C:\Users\User\.virtualenvs\nguyena-QPBkFrkQ\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\User\.virtualenvs\nguyena-QPBkFrkQ\lib\site-packages\django\views\generic\base.py", line 84, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\User\.virtualenvs\nguyena-QPBkFrkQ\lib\site-packages\django\contrib\auth\mixins.py", line 73, in dispatch
return super().dispatch(request, *args, **kwargs)
File "C:\Users\User\.virtualenvs\nguyena-QPBkFrkQ\lib\site-packages\django\views\generic\base.py", line 119, in dispatch
return handler(request, *args, **kwargs)
File "C:\Users\User\.virtualenvs\nguyena-QPBkFrkQ\lib\site-packages\django\views\generic\edit.py", line 184, in post
return super().post(request, *args, **kwargs)
File "C:\Users\User\.virtualenvs\nguyena-QPBkFrkQ\lib\site-packages\django\views\generic\edit.py", line 153, in post
return self.form_valid(form)
File "C:\Users\User\Documents\django projects\nguyena\internalcontrol\views.py", line 231, in form_valid
return super().form_valid(form)
File "C:\Users\User\.virtualenvs\nguyena-QPBkFrkQ\lib\site-packages\django\views\generic\edit.py", line 136, in form_valid
return super().form_valid(form)
File "C:\Users\User\.virtualenvs\nguyena-QPBkFrkQ\lib\site-packages\django\views\generic\edit.py", line 65, in form_valid
return HttpResponseRedirect(self.get_success_url())
File "C:\Users\User\.virtualenvs\nguyena-QPBkFrkQ\lib\site-packages\django\http\response.py", line 538, in __init__
self["Location"] = iri_to_uri(redirect_to)
File "C:\Users\User\.virtualenvs\nguyena-QPBkFrkQ\lib\site-packages\django\utils\encoding.py", line 139, in iri_to_uri
return quote(iri, safe="/#%[]=:;$&()+,!?*#'~")
File "c:\users\user\appdata\local\programs\python\python39\lib\urllib\parse.py", line 871, in quote
return quote_from_bytes(string, safe)
File "c:\users\user\appdata\local\programs\python\python39\lib\urllib\parse.py", line 896, in quote_from_bytes
raise TypeError("quote_from_bytes() expected bytes")
TypeError: quote_from_bytes() expected bytes
[18/Apr/2022 17:08:13] "POST /task_create/ HTTP/1.1" 500 125026
I think the problem is the final address to which the user should be redirected.
To solve this problem, first edit the get_absolute_url function to return a string, not a tuple.
If it still does not work, try adding a success_url to the view with reverse_lazy
I have several functional views that are passed parameters. I'm trying to write tests to check the url, status, etc. So far I'm starting with just testing the URL name. I have some querysets in the view and it looks from the traceback that I need to define objects for the queries in my setUp
urls
app_name = 'gradebook'
urlpatterns = [
path('updatesinglegrade/<int:assess_pk>/<int:class_pk>/<int:grade_pk>/',
views.updatesinglegrade, name='updatesinglegrade'),
]
view
def updatesinglegrade(request, assess_pk, class_pk, grade_pk):
grade = Grade.objects.get(id=grade_pk)
gradescale = GradeBookSetup.objects.get(user=request.user)
scale_choice = grade_scale_choice(gradescale.scale_mode)
form = UpdateGradeForm(gradescale=scale_choice)
context = {'form': form}
context['grade'] = grade
if request.method == 'POST':
form = UpdateGradeForm(
request.POST, gradescale=scale_choice)
if form.is_valid():
cd = form.cleaned_data
grade.score = cd['score']
grade.save()
return redirect('gradebook:assessdetail', assess_pk, class_pk)
else:
return render(request, "gradebook/grade_single_form.html", context)
else:
return render(request, "gradebook/grade_single_form.html", context)
test
class UpdateSingleGradeTests(TestCase):
def setUp(self):
self.user = CustomUser.objects.create_user(
username='tester',
email='tester#email.com',
password='tester123'
)
login = self.client.login(username='tester', password='tester123')
def test_updatesinglegrade_url_name(self):
response = self.client.get(reverse('gradebook:updatesinglegrade', kwargs={
'assess_pk': 1, 'class_pk': 2, 'grade_pk': 3}))
self.assertEqual(response.status_code, 200)
traceback
======================================================================
ERROR: test_updatesinglegrade_url_name (gradebook.tests.UpdateSingleGradeTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\Doug\OneDrive\django\gradebook\gradebook\tests.py", line 102, in test_updatesinglegrade_url_name
response = self.client.get(reverse('gradebook:updatesinglegrade', kwargs={
File "C:\Users\Doug\.virtualenvs\gradebook-6RQSg7dk\lib\site-packages\django\test\client.py", line 742, in get
response = super().get(path, data=data, secure=secure, **extra)
File "C:\Users\Doug\.virtualenvs\gradebook-6RQSg7dk\lib\site-packages\django\test\client.py", line 396, in get
return self.generic('GET', path, secure=secure, **{
File "C:\Users\Doug\.virtualenvs\gradebook-6RQSg7dk\lib\site-packages\django\test\client.py", line 473, in generic
return self.request(**r)
File "C:\Users\Doug\.virtualenvs\gradebook-6RQSg7dk\lib\site-packages\django\test\client.py", line 719, in request
self.check_exception(response)
File "C:\Users\Doug\.virtualenvs\gradebook-6RQSg7dk\lib\site-packages\django\test\client.py", line 580, in check_exception
raise exc_value
File "C:\Users\Doug\.virtualenvs\gradebook-6RQSg7dk\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Doug\.virtualenvs\gradebook-6RQSg7dk\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Doug\.virtualenvs\gradebook-6RQSg7dk\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "C:\Users\Doug\.virtualenvs\gradebook-6RQSg7dk\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "C:\Users\Doug\OneDrive\django\gradebook\gradebook\views.py", line 1030, in updatesinglegrade
grade = Grade.objects.get(id=grade_pk)
File "C:\Users\Doug\.virtualenvs\gradebook-6RQSg7dk\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Doug\.virtualenvs\gradebook-6RQSg7dk\lib\site-packages\django\db\models\query.py", line 435, in get
raise self.model.DoesNotExist(
gradebook.models.Grade.DoesNotExist: Grade matching query does not exist.
----------------------------------------------------------------------
Ran 6 tests in 1.527s
FAILED (errors=1)
I can create a Grade object as the traceback indicates, but my Grade model depends on five ForeignKeys, one of which has another ForeignKey:
model
class Grade(models.Model):
score = models.CharField(max_length=3, blank=True, default="INC")
user = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
assessment = models.ForeignKey(
Assessment, on_delete=models.CASCADE, null=True, blank=True)
objective = models.ForeignKey(
Objective, on_delete=models.CASCADE, blank=True)
student = models.ForeignKey(Student, on_delete=models.CASCADE)
cblock = models.ForeignKey(Classroom, on_delete=models.CASCADE, default=1)
time_created = models.DateField(
auto_now=False, auto_now_add=False, default=timezone.now)
working test
from .models import CustomUser, Grade, Student, Course, Classroom, Objective, Assessment, GradeBookSetup
class UpdateSingleGradeTests(TestCase):
def setUp(self):
self.user = CustomUser.objects.create_user(
username='tester',
email='tester#email.com',
password='tester123',
is_teacher=True,
)
studentuser = CustomUser.objects.create(
username='student_test'
)
stud = Student.objects.create(
user=studentuser,
student_number='111222'
)
course = Course.objects.create(
user=self.user
)
classroom = Classroom.objects.create(
user=self.user,
course=course
)
# classroom.students.set(stud)
objective = Objective.objects.create(
user=self.user,
course=course
)
assessment = Assessment.objects.create(
user=self.user,
course=course
)
# assessment.objectives.set(objective)
login = self.client.login(username='tester', password='tester123')
grade = Grade.objects.create(
id=3,
user=self.user,
student=stud,
objective=objective,
cblock=classroom,
assessment=assessment
)
gbs = GradeBookSetup.objects.create(
id=1,
user=self.user
)
def test_updatesinglegrade_url_name(self):
response = self.client.get(reverse('gradebook:updatesinglegrade', kwargs={
'assess_pk': 1, 'class_pk': 2, 'grade_pk': 3}))
self.assertEqual(response.status_code, 200)
Does this mean that I need to create 6 objects in the setUp? I got it work for this test, and I'm wondering if this is something I will have to add to many of my class TestCase (which have similar objects/models involved)? Or perhaps there is a more efficient way of doing this?
The short answer is, yes, non-null ForeignKey fields must be populated in tests because they are non-null.
There are several ways to do this:
Call objects.create() on each model. This can be very tedious and time consuming.
Use a fixture. Creating the JSON files can also be tedious, but fortunately, you can use the Django admin to do data entry and then ./manage.py dumpdata to generate the JSON.
Use a 3rd party library such as factory-boy or model-bakery. These libraries can generate random data for fields that don't really matter for testing other than that they have a value. If you need a specific value for a certain test, then you can provide it.
You can create fixture and use it in your TestCase class
docs on subject: https://docs.djangoproject.com/en/3.2/topics/testing/tools/#fixture-loading
class UpdateSingleGradeTests(TestCase):
fixtures = ['fixture.json']
I tried to implement SSO for my Django project.
when I tried to login form SP ('/sso/login') I got the correct response from my IDP but on SP side 'sso/acs' it throwing a validation error.
{'is_superuser': ["'true' value must be either True or False."], 'is_staff': ["'true' value must be either True or False."]}
When I removed is_staff and is_superuser from SAML_ATTRIBUTE_MAPPING.
SSO is working.
Project packages -
django 1.11,
postgress,
djangosaml2idp 0.5.0, for IDP
djangosaml2 0.17.2 for SP
My setting for SP:
SAML_USE_NAME_ID_AS_USERNAME = True
SAML_DJANGO_USER_MAIN_ATTRIBUTE = 'email'
SAML_DJANGO_USER_MAIN_ATTRIBUTE_LOOKUP = '__iexact'
SAML_CREATE_UNKNOWN_USER = False
SAML_LOGOUT_REQUEST_PREFERRED_BINDING = saml2.BINDING_HTTP_POST
SAML_ATTRIBUTE_MAPPING = {
# SAML : DJANGO
# Must also be present in attribute-maps!
'email': ('email', ),
'username': ('username', ),
'account_id': ('account_id', ),
'is_staff': ('is_staff', ),
'is_superuser': ('is_superuser', ),
}
Error Traceback:
Traceback (most recent call last):
File "/Users/lite/Virtual_Envs/spEnv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Users/lite/Virtual_Envs/spEnv/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/lite/Virtual_Envs/spEnv/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/lite/Virtual_Envs/spEnv/lib/python3.6/site-packages/django/views/decorators/http.py", line 40, in inner
return func(request, *args, **kwargs)
File "/Users/lite/Virtual_Envs/spEnv/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/Users/lite/Virtual_Envs/spEnv/lib/python3.6/site-packages/djangosaml2/views.py", line 316, in assertion_consumer_service
create_unknown_user=create_unknown_user)
File "/Users/lite/Virtual_Envs/spEnv/lib/python3.6/site-packages/django/contrib/auth/__init__.py", line 73, in authenticate
user = backend.authenticate(request, **credentials)
File "/Users/lite/Virtual_Envs/spEnv/lib/python3.6/site-packages/djangosaml2/backends.py", line 107, in authenticate
create_unknown_user, main_attribute, attributes, attribute_mapping)
File "/Users/lite/Virtual_Envs/spEnv/lib/python3.6/site-packages/djangosaml2/backends.py", line 154, in get_saml2_user
return self._get_saml2_user(main_attribute, attributes, attribute_mapping)
File "/Users/lite/Virtual_Envs/spEnv/lib/python3.6/site-packages/djangosaml2/backends.py", line 188, in _get_saml2_user
user = self.update_user(user, attributes, attribute_mapping)
File "/Users/lite/Virtual_Envs/spEnv/lib/python3.6/site-packages/djangosaml2/backends.py", line 250, in update_user
user.save()
File "/Users/lite/projects/djangosaml2/sample-sp/sp/models.py", line 105, in save
self.full_clean()
File "/Users/lite/Virtual_Envs/spEnv/lib/python3.6/site-packages/django/db/models/base.py", line 1203, in full_clean
raise ValidationError(errors)
django.core.exceptions.ValidationError: {'is_superuser': ["'true' value must be either True or False."], 'is_staff': ["'true' value must be either True or False."]}
I tried with different DBs and with or without overriding Django user model.
when I tried with default (auth.models) User model sso is working with Postgres and sqlite backend but when I override the User model for my custom user model it throwing above error.
I think on SP is not typecasting IDP response, it just parsing and passing all attributes as strings and while saving on DB it causing a validation error. but why it is working with the default Django User model ?
Application User model
class User(AbstractBaseUser, PermissionsMixin):
username = models.CharField(max_length=15, unique=True,)
email = models.EmailField(max_length=123, blank=False, null=False, unique=True)
is_staff = models.BooleanField(default=False, db_index=True)
objects = UserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
def clean(self):
errors = {}
users = get_user_model().objects.filter(username__iexact=self.username)
if len(users) > 1:
errors.setdefault('username', []).append(f'{self.username} not available')
if errors:
raise ValidationError(errors)
def save(self, force_insert=False, force_update=False, using=None,
update_fields=None):
self.full_clean()
return super().save(force_insert, force_update, using, update_fields)
Had the same issue, seems like this is something to do with djangosaml2idp implementation.
From the example code in the source, you should add the following code in the User app view.py file (or in the main project folder, if you didn't customize the User model).
#receiver(pre_user_save, sender=User)
def custom_update_user(sender, instance, attributes, user_modified, **kargs):
""" Default behaviour does not play nice with booleans encoded in SAML as u'true'/u'false'.
This will convert those attributes to real booleans when saving.
"""
for k, v in attributes.items():
u = set.intersection(set(v), set([u'true', u'false']))
if u:
setattr(instance, k, u.pop() == u'true')
return True
Source:
custom_update_user
I am attempting to create a POST endpoint using DRF ListSerializer to create a list of LogLevel objects.
I have tried to serialize the foreign key using PrimaryKeyRelatedField without success.
models.py
relevant fields for LogLevel model. note foreign key to node model
#associated node
node = models.ForeignKey(Node, on_delete=models.DO_NOTHING,
related_name="log_levels")
#logger name
name = models.CharField(max_length=32, choices=LOGGERS)
# Current log level
level = models.IntegerField(default=INFO,
choices=LOG_LEVELS)
# Timestamps
created_datetime = models.DateTimeField(auto_now_add=True)
updated_datetime = models.DateTimeField(auto_now=True,
blank=True, null=True)
serializers.py
class LogLevelListSerializer(serializers.ListSerializer):
def create(self, validated_data):
log_levels = [LogLevel(**item) for item in validated_data]
levels = LogLevel.objects.bulk_create(log_levels)
return levels
class LogLevelCreateUpdateSerializer(serializers.ModelSerializer):
class Meta:
model = LogLevel
fields = "__all__"
list_serializer_class = LogLevelListSerializer
LogLevel view
class LogLevelList(MethodSerializerMixin,
generics.ListCreateAPIView):
"""
Log Level list API Endpoint.
"""
method_serializer_classes = {
("POST",): LogLevelCreateUpdateSerializer
}
def get_queryset(self):
"""
Queryset to use for endpoint.
"""
return LogLevel.objects.all()
def get_serializer(self, *args, **kwargs):
"""
Return the serializer instance that should be used for validating and
deserializing input, and for serializing output.
"""
serializer_class = self.get_serializer_class()
kwargs['context'] = self.get_serializer_context()
# check if many is required
if "data" in kwargs:
data = kwargs["data"]
# check if many is required
if isinstance(data, list):
kwargs["many"] = True
return serializer_class(*args, **kwargs)
MethodSerializerMixin
from rest_framework import exceptions
class MethodSerializerMixin(object):
"""
Utility class to apply a different serializer class depending
on the request method.
For example:
method_serializer_classes = {
("GET", ): MyModelListViewSerializer,
("PUT", "PATCH"): MyModelCreateUpdateSerializer
}
"""
method_serializer_classes = None
def get_serializer_class(self):
assert self.method_serializer_classes is not None, (
f"Expected view {self.__class__.__name__} should contain "
f"method_serializer_classes to get right serializer class."
)
for methods, serializer_cls in self.method_serializer_classes.items():
if self.request.method in methods:
return serializer_cls
raise exceptions.MethodNotAllowed(self.request.method)
Im passing in a json list of simple objects in the request. node is the foreign key id:
[{
"name": "logger1",
"level": 2,
"node": 1
},
{
"name": "logger2",
"level": 3,
"node": 1
}]
I expect the objects to be created and displayed to the client with success status. Currently, the objects are created in the db successfully but a 500: Server Error is returned and this is the stacktrace I see on Django server:
Internal Server Error: /api/clustering/loglevel/set/
Traceback (most recent call last):
File "/opt/cisco/env/iris/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/opt/cisco/env/iris/lib/python3.6/site-packages/django/core/handlers/base.py", line 145, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/opt/cisco/env/iris/lib/python3.6/site-packages/django/core/handlers/base.py", line 143, in _get_response
response = response.render()
File "/opt/cisco/env/iris/lib/python3.6/site-packages/django/template/response.py", line 106, in render
self.content = self.rendered_content
File "/opt/cisco/env/iris/lib/python3.6/site-packages/rest_framework/response.py", line 72, in rendered_content
ret = renderer.render(self.data, accepted_media_type, context)
File "/opt/cisco/env/iris/lib/python3.6/site-packages/rest_framework/renderers.py", line 724, in render
context = self.get_context(data, accepted_media_type, renderer_context)
File "/opt/cisco/env/iris/lib/python3.6/site-packages/rest_framework/renderers.py", line 697, in get_context
'post_form': self.get_rendered_html_form(data, view, 'POST', request),
File "/opt/cisco/env/iris/lib/python3.6/site-packages/rest_framework/renderers.py", line 520, in get_rendered_html_form
return self.render_form_for_serializer(serializer)
File "/opt/cisco/env/iris/lib/python3.6/site-packages/rest_framework/renderers.py", line 528, in render_form_for_serializer
serializer.data,
File "/opt/cisco/env/iris/lib/python3.6/site-packages/rest_framework/serializers.py", line 765, in data
ret = super(ListSerializer, self).data
File "/opt/cisco/env/iris/lib/python3.6/site-packages/rest_framework/serializers.py", line 266, in data
self._data = self.get_initial()
File "/opt/cisco/env/iris/lib/python3.6/site-packages/rest_framework/serializers.py", line 600, in get_initial
return self.to_representation(self.initial_data)
File "/opt/cisco/env/iris/lib/python3.6/site-packages/rest_framework/serializers.py", line 683, in to_representation
self.child.to_representation(item) for item in iterable
File "/opt/cisco/env/iris/lib/python3.6/site-packages/rest_framework/serializers.py", line 683, in <listcomp>
self.child.to_representation(item) for item in iterable
File "/opt/cisco/env/iris/lib/python3.6/site-packages/rest_framework/serializers.py", line 527, in to_representation
ret[field.field_name] = field.to_representation(attribute)
File "/opt/cisco/env/iris/lib/python3.6/site-packages/rest_framework/relations.py", line 257, in to_representation
return value.pk
AttributeError: 'int' object has no attribute 'pk'
python==3.6
django==2.2.2
drf==3.8.2
The serializer.data property is only valid if you have a saved an instance to the serializer.
Either call serializer.save() or use serializer.validated_data to access data prior to saving.
Checkout this link for further information.
Had to handle this error by updating to_representation method on the PrimaryKeyRelatedField class
class NodePrimaryKeyField(serializers.PrimaryKeyRelatedField):
"""
Custom DRF serializer field for proper handling of
Node Foreign Key by ListSerializer on validation error
"""
def to_representation(self, value):
"""
Return pk value of serialized Node object
if available else return given ID value
"""
if self.pk_field is not None:
return self.pk_field.to_representation(value.pk)
return getattr(value, 'pk', value)
I am designing a simple MCQ application using the ManyToOne relationship from django website, django version 1.6.1. I have an inline admin form which supposedly allows me to add / edit answers to a given question from the same changeform. However, if after saving a question with its answers once, i am unable to edit / add answers from the same form and get a MultiValueDictKeyError. My models are:
class SBA (models.Model):
question = models.TextField(blank=False)
system = models.CharField(max_length=3, choices=pacscon.System.which_system)
case = models.ForeignKey('pacscon.Patient')
created = models.DateField(auto_now_add=True)
reference = models.TextField(blank=True)
def __unicode__(self):
return self.question
class Answer(models.Model):
id = models.AutoField(primary_key=True)
body = models.TextField()
correct = models.BooleanField(default=False)
sba = models.ForeignKey(SBA, null=True)
def __unicode__(self):
return self.body
the admin.py looks like this :
from models import Answer, SBA, Mnemonic
#from django import forms
from django.contrib import admin
class AnswerInline(admin.StackedInline):
model = Answer
extra = 2
class SBAAdmin(admin.ModelAdmin):
fieldsets = (
(None, {
'fields': ('question', 'system', 'case', 'reference')
}),
)
inlines = [
AnswerInline,
]
readonly_fields = ('created',)
admin.site.register(SBA, SBAAdmin)
admin.site.register(Answer)
admin.site.register(Mnemonic)
And the error message is :
MultiValueDictKeyError at /admin/knowledge/sba/1/
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in wrapper
432. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
99. response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func
52. response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py" in inner
198. return view(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapper
29. return bound_func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
99. response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in bound_func
25. return func(self, *args2, **kwargs2)
File "/usr/local/lib/python2.7/dist-packages/django/db/transaction.py" in inner
339. return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in change_view
1229. if all_valid(formsets) and form_validated:
File "/usr/local/lib/python2.7/dist-packages/django/forms/formsets.py" in all_valid
415. if not formset.is_valid():
File "/usr/local/lib/python2.7/dist-packages/django/forms/formsets.py" in is_valid
292. err = self.errors
File "/usr/local/lib/python2.7/dist-packages/django/forms/formsets.py" in errors
267. self.full_clean()
File "/usr/local/lib/python2.7/dist-packages/django/forms/formsets.py" in full_clean
314. form = self.forms[i]
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py" in __get__
49. res = instance.__dict__[self.func.__name__] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/forms/formsets.py" in forms
133. forms = [self._construct_form(i) for i in xrange(self.total_form_count())]
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in _construct_form
848. form = super(BaseInlineFormSet, self)._construct_form(i, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in _construct_form
564. pk = self.data[pk_key]
File "/usr/local/lib/python2.7/dist-packages/django/utils/datastructures.py" in __getitem__
301. raise MultiValueDictKeyError(repr(key))
Exception Type: MultiValueDictKeyError at /admin/knowledge/sba/1/
Exception Value: "u'answer_set-0-id'"
I have searched similar requests on google and stackoverflow and my django package is fully upto date (including admin inline templates as suggested in some other answers). Will appreciate any help. Thanks
P.S This is almost certainly a bug because I can replicate the problem on official django "Polls" tutorial which uses a similar ManyToOne relationship model.
In Django <= 1.7.3, I needed to change the primary key field of my model to AutoField:
wrong:
class Answer(models.Model):
id = IntegerField(primary_key=True)
correct:
class Answer(models.Model):
id = AutoField(primary_key=True)
See
https://code.djangoproject.com/ticket/15665
I have similar issue which was solved by update django-grappelli. If you did not use it, check your packages maybe you find what can affect work of admin site.
pip freeze | grep django- will show you current packages with versions
This answer https://stackoverflow.com/a/20246225/554807 has the explanation.
The best fix is actually to get silverfix's branch of nested-inlines: https://github.com/silverfix/django-nested-inlines
This has the fix mentioned in that answer, and others.
After migrating an application from Django==1.4.20 to Django==1.8.3 i had an outdated templates_django/admin/edit_inline/stacked.html and templates_django/admin/edit_inline/tabular.html files.
So i have just removed them and everything works fine now.
You can also upgrade them to your current django version instead of deleting.