views:

124

answers:

1

I can't figure out how I'm supposed to order these models that have a M2M relationship.

When I try to syncdb, I can't because the Model related model is not in the namespace yet, so I get: NameError: name 'FavoriteQuestion' is not defined

If I switch places, I get: NameError: name 'UserProfile' is not defined

class UserProfile(models.Model):  
    """User Profile customizations"""  
    user = models.ForeignKey(User, unique=True)  
    favorite_questions =  models.ManyToManyField(Question, through=FavoriteQuestion,  
                                                 related_name='favorited_by')  
    badges = models.ManyToManyField(Badge, through=Award,  
                                    related_name='awarded_to')  


class FavoriteQuestion(models.Model):  
    """A favorite Question of a User."""  
    question      = models.ForeignKey(Question)  
    user          = models.ForeignKey(UserProfile, related_name='user_favorite_questions')  
    added_at = models.DateTimeField(default=datetime.datetime.now)

Her is the TraceBack:

$ python manage.py syncdb
Traceback (most recent call last):
File "manage.py", line 11, in
execute_manager(settings)
File "/usr/local/lib/python2.6/site-packages/django/core/management/init.py", line 439, in execute_manager
utility.execute()
File "/usr/local/lib/python2.6/site-packages/django/core/management/init.py", line 380, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.6/site-packages/django/core/management/base.py", line 195, in run_from_argv
self.execute(*args, **options.dict)
File "/usr/local/lib/python2.6/site-packages/django/core/management/base.py", line 221, in execute
self.validate()
File "/usr/local/lib/python2.6/site-packages/django/core/management/base.py", line 249, in validate
num_errors = get_validation_errors(s, app)
File "/usr/local/lib/python2.6/site-packages/django/core/management/validation.py", line 28, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "/usr/local/lib/python2.6/site-packages/django/db/models/loading.py", line 131, in get_app_errors
self._populate()
File "/usr/local/lib/python2.6/site-packages/django/db/models/loading.py", line 58, in _populate
self.load_app(app_name, True)
File "/usr/local/lib/python2.6/site-packages/django/db/models/loading.py", line 74, in load_app
models = import_module('.models', app_name)
File "/usr/local/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
import(name)
File "/Users/Bryan/work/APPNAME/forum/models.py", line 434, in
class FavoriteQuestion(models.Model):
File "/Users/Bryan/work/APPNAME/forum/models.py", line 437, in FavoriteQuestion
user = models.ForeignKey(UserProfile, related_name='user_favorite_questions')
NameError: name 'UserProfile' is not defined

A: 

You don't need to create the favourite_question field on the UserProfile.

As long as each FavouriteQuestion has a user associated with it the model instance API will give you access to a list of favourite questions for a user.

Andy Hume