Askbot fails when using Nginx+Gunicorn
In my attempt to integrate Askbot with an already running Django site, I had to make it run under current setup (nginx+gunicorn), but it keeps failing, been trying to figure out what's wrong but now I'm pretty much clueless. There are some speculation about the python paths, gunicorn workers (gevent) and other stuff, but even after trying to change all of them, it still failed. The error was:
'NoneType' object has no attribute 'field_name'
Here's the complete stack trace: https://gist.github.com/4363587
This is not happening when I'm running the dev server (manage.py runserver).
Appreciate any help and thanks for making such an awesome Q&A Platform!.
Update This seems to be triggered by Group model, a simple query would break the script with the same error:
from askbot.models.user import Group
Group.objects.filter(user=[any_user])
The same code would run just fine from ./manage.py shell
Update This error is somehow related to fetching ManyToMany relationship. To trigger it, try running this on dev and gunicorn:
user = User.objects.get(username='admin') # for example
user.get_primary_group()
When running that code on dev, I get fine result, however gunicorn shows:
'NoneType' object has no attribute 'field_name'
If you follow the stack trace, you'll get to this function to map m2m attribute in Django, I tried to inject this to compare the result of f.rel.to == related.model
for get_primary_group()
, here's what I get with dev server:
<django.contrib.auth.models.User> == <django.contrib.auth.models.User>
And here's what I get with gunicorn:
<django.contrib.auth.models.User> != <django.contrib.auth.models.User>
If I use dir()
on both scenarios, it turned out that the User model on dev server has a lot of properties and map the complete list of related_name (my apps and askbot), in gunicorn, there is just default User properties such as change_password.
You can also try to run this code in both dev and gunicorn, the result on both server will be different:
from django.contrib.auth.models import User
print dir(User.groups.field.rel.through._meta.fields[1].rel.to) # Will return User model
But strangely, this code shows fine and same result on both server:
from django.contrib.auth.models import User
print dir(User)
First I thought it was a migration problem, but I already triggered migration many times and checked my migration list, it was completed.
Comments
Looks like your database is not migrated. Try
python manage.py migrate
.@Evgeny I triggered migrations many times before and checked my migration list, it was all completed, I added more details in the question. Thanks!.
For the
get_primary_group()
usage example - did you import user model fromaskbot.models
ordjango.contrib.auth.models
?I use django.contrib.auth.models, the reason I use that example is because that particular code is also exist in Askbot template.
Try importing from
askbot.models
, that's where the methods are added to the user model. Can you email me at support@askbot.com I'll try to help you further.