开发者

Creating Custom User Backends in Django

I have a Shops model and would like each shop to be able to login to my application. Following as best I can the guide at http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/ and various other googlings, I've got part of the way there, but I've run into a problem. When I try to login as a shop, I get the following error:

OperationalError at /login/
(1054, "Unknown column 'shops.user_ptr_id' in 'field list'")

Shops model:

class Shops(User):
  shop_id = models.AutoField(primary_key=True)
  shop_code = models.CharField(unique=True, max_length=5)
  shop_type_fk = models.ForeignKey(ShopTypes,
        开发者_Python百科                           null=True,
                                   db_column='shop_type_id',
                                   blank=True)
  address_fk = models.ForeignKey(Addresses, db_column='address_id')
  phone_number = models.CharField(max_length=30)
  #email = models.EmailField(max_length=255, blank=True)
  description = models.TextField(blank=True)
  does_gift_aid = models.NullBooleanField(null=True, blank=True)
  objects = UserManager()
  class Meta:
    db_table = u'shops'

I've sync'd the database, so surely it should have made the column user_ptr_id. Does anyone know where I'm going wrong?


"I've sync'd the database, so surely it should have made the column user_ptr_id."

What makes you think that? Especially in light of this clear statement in the docs for syncdb:

Syncdb will not alter existing tables

syncdb will only create tables for models which have not yet been installed. It will never issue ALTER TABLE statements to match changes made to a model class after installation. Changes to model classes and database schemas often involve some form of ambiguity and, in those cases, Django would have to guess at the correct changes to make. There is a risk that critical data would be lost in the process.

If you have made changes to a model and wish to alter the database tables to match, use the sql command to display the new SQL structure and compare that to your existing table schema to work out the changes.


It does sound like you had an existing shops table before changing it to inherit from User (as Daniel notes), and syncdb does not update the schema for existing tables.

You need to drop the table and then run syncdb, if possible. Otherwise you need to go into your database and add the user_ptr_id field manually, if you know how to do that. The definition should look something like this:

"user_ptr_id" integer NOT NULL UNIQUE REFERENCES "auth_user" ("id")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜