I have a problem, that I have resolved, with different results I get with a project between using it manage.py runserver
and via WSGI and Apache.
The problematic piece of code is (I’ll post the models if they are needed, but I think the general schematic is obvious enough):
At this point there are no database records (except the account
record).
views.py
…
user = get_user_model().objects.create_user(username=username, email=email, password=password)
…
user.profile.account = account
user.profile.member = member
user.profile.member.account = account
user.profile.member.save()
user.save()
When running this with runserver
this works exactly as I expect, the member
, profile
and user
records are created, and they are linked to the account
(profile
is created automagically because it is a one-to-one relationship with user
).
However, when I run it with WSGI and Apache, although there is no error in the saving and response, data is missing in the HTML display that originates in the member
record. When I check the database tables I see that everything has saved as expected, except the user.profile.member_id
field is empty (this is not in the model, it is inserted into the database by implication of user.profile.member
relationship).
So, to resolve this problem:
views.py
…
user = get_user_model().objects.create_user(username=username, email=email, password=password)
…
user.profile.account = account
user.profile.member = member
user.profile.member.account = account
user.profile.member.save()
'''new line'''
user.profile.member_id = user.profile.member.id
user.save()
So, I’d like to know whether the initial way I did this is correct, or it’s just a fluke that it worked and the “fix” is the correct way.
I’m thinking that perhaps there is some difference in the Django or Python version between the different behaviours. Although I believe they are the same, I cannot work out a way to test that (ie some output on the template or the terminal/Apache error log that confirms the versions).
Thanks