User, Django



  • Hello. Decided to expand the base model User for Django (1.9.7) (appendix, appendix field balance). Added. AUTH_PROFILE_MODULE Total settings.py♪ Created a model in models.py:

    from django.contrib.auth.models import AbstractBaseUser, User
    from django.db import models
    from decimal import Decimal
    

    class MyUser(AbstractBaseUser):
    user = models.ForeignKey(User)
    balance = models.DecimalField(..., max_digits=7, decimal_places=0, default=Decimal('0'))

    In built-in forms, copying and registration work. But if you'd like to go to bed user.balance There's nothing going on. Also, if you try to remove the accustom from the admink, there's a mistake:

    (1146, "Table 'mydatabase.project_myuser' doesn't exist."

    The fields. balance There's no Adminke either.
    Please help me understand.



  • You don't need an external key to the laser, if you inherit from it. AbstractBaseUser
    And you don't have to use this abstract class. If you just want to add fields to the model, you can inherit from it. AbstractUser
    AbstractBaseUser If you want to completely redesign the laser model.

    from django.contrib.auth.models import AbstractUser
    

    class MyUser(AbstractUser):
    balance = models.DecimalField(max_digits=7, decimal_places=0, default=Decimal('0'))

    And write in settings.py Redefined model:
    AUTH_USER_MODEL = "<название приложения>.MyUser"

    (1146, "Table 'mydatabase.project_myuser' doesn't exist")

    This mistake indicates that the tables are not in the database, most likely you forgot to make migration. Do it after you fix the model.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2