hi, inspect your typo and Organization model.

On Tue, Nov 26, 2019, 16:31 Ahmad Saki <[email protected]> wrote:

> Dear,
> Thanks for your response.
> I use the foreign key field 'orgid' or not but got the same error. If I
> want to use this then where is my fault for this error!
>
>
> On Tuesday, November 26, 2019 at 12:11:58 PM UTC+6, Integr@te System
> wrote:
>>
>> Hi man,
>>
>> Check 'class UserManager(): ......orgid = org_obj '
>>
>>
>>
>>
>> On Tue, Nov 26, 2019, 01:40 Ahmad Saki <[email protected]> wrote:
>>
>>> My custom authentication model in django:
>>>
>>> from django.db import models
>>> from django.contrib.auth.models import (
>>> AbstractBaseUser, BaseUserManager
>>> )
>>> from organization.models import Organization
>>>
>>>
>>> class UserManager(BaseUserManager):
>>> # use_in_migrations = True
>>>
>>> # python manage.py createsuperuser
>>> def create_user(self, orgid, username, email,  password=None, 
>>> is_admin=False, is_staff=False, is_active=True):
>>>     if not orgid:
>>>         raise ValueError("Organization of user must not empty")
>>>     elif not username:
>>>         raise ValueError("User must have an username")
>>>     elif not email:
>>>         raise ValueError("User must have an email address")
>>>
>>>     org_obj = Organization.objects.all(orgid=orgid)
>>>     user_obj = self.model(
>>>         orgid=org_obj,
>>>         username=username,
>>>         email = self.normalize_email(email),
>>>         password=password
>>>         )
>>>     user_obj.set_password(password)
>>>     user_obj.admin=is_admin
>>>     user_obj.staff=is_staff
>>>     user_obj.active=is_active
>>>     user_obj.save(using=self._db)
>>>     return user_obj
>>>
>>> # python manage.py createsuperuser
>>> def create_superuser(self, orgid, username, email, password=None):
>>>     user = 
>>> self.create_user(orgid,username,email,password=password,is_admin=True,is_staff=True,is_active=True)
>>>     return user
>>>
>>> def create_staffuser(self, orgid, username, email, password=None):
>>>     user = 
>>> self.create_user(orgid,username,email,password=password,is_admin=False,is_staff=True,is_active=True)
>>>     return user
>>>
>>> class User(AbstractBaseUser):
>>> orgid       = models.ForeignKey(Organization, max_length=6, 
>>> on_delete=models.CASCADE)
>>> username    = models.CharField(primary_key=True, max_length=50)
>>> email       = models.EmailField(max_length=255, unique=True, null=False, 
>>> blank=False)
>>> admin       = models.BooleanField(default=False)
>>> staff       = models.BooleanField(default=False)
>>> active      = models.BooleanField(default=True)
>>> date_joined = models.DateTimeField(auto_now_add=True)
>>>
>>> objects = UserManager()
>>>
>>> USERNAME_FIELD = "username"
>>> # REQUIRED_FIELDS must contain all required fields on your User model,
>>> # but should not contain the USERNAME_FIELD or password as these fields 
>>> will always be prompted for.
>>> REQUIRED_FIELDS = ['orgid','email']
>>>
>>> class Meta:
>>>     app_label = "user"
>>>     db_table = "user"
>>>
>>> def __str__(self):
>>>     return self.username
>>>
>>> def get_full_name(self):
>>>     return self.username
>>>
>>> def get_short_name(self):
>>>     return self.username
>>>
>>> # this methods are require to login super user from admin panel
>>> def has_perm(self, perm, obj=None):
>>>     return self.is_staff
>>>
>>> # this methods are require to login super user from admin panel
>>> def has_module_perms(self, app_label):
>>>     return self.is_staff
>>>
>>> @property
>>> def is_admin(self):
>>>     return self.admin
>>>
>>> @property
>>> def is_staff(self):
>>>     return self.staff
>>>
>>> @property
>>> def is_active(self):
>>>     return self.active
>>>
>>>
>>>
>>> When I create migrations it creates:
>>>
>>> from django.db import migrations, models
>>> import django.db.models.deletion
>>> import user.models
>>>
>>>
>>> class Migration(migrations.Migration):
>>>
>>> initial = True
>>>
>>> dependencies = [
>>>     ('organization', '0001_initial'),
>>> ]
>>>
>>> operations = [
>>>     migrations.CreateModel(
>>>         name='User',
>>>         fields=[
>>>             ('password', models.CharField(max_length=128, 
>>> verbose_name='password')),
>>>             ('last_login', models.DateTimeField(blank=True, null=True, 
>>> verbose_name='last login')),
>>>             ('username', models.CharField(max_length=50, primary_key=True, 
>>> serialize=False)),
>>>             ('email', models.EmailField(max_length=255, unique=True)),
>>>             ('admin', models.BooleanField(default=False)),
>>>             ('staff', models.BooleanField(default=False)),
>>>             ('active', models.BooleanField(default=True)),
>>>             ('date_joined', models.DateTimeField(auto_now_add=True)),
>>>             ('orgid', models.ForeignKey(max_length=6, 
>>> on_delete=django.db.models.deletion.CASCADE, 
>>> to='organization.Organization')),
>>>         ],
>>>         options={
>>>             'db_table': 'user',
>>>         },
>>>         managers=[
>>>             ('objects', user.models.UserManager()),
>>>         ],
>>>     ),
>>> ]
>>>
>>>
>>>
>>> But when I want to migrate it gives me an error below. Please help me
>>> anyone..
>>>
>>> Operations to perform:
>>>   Apply all migrations: admin, auth, contenttypes, organization, sessions, 
>>> user
>>> Running migrations:
>>>   Applying admin.0004_auto_20191118_2315...Traceback (most recent call 
>>> last):
>>>   File "manage.py", line 21, in <module>
>>>     main()
>>>   File "manage.py", line 17, in main
>>>     execute_from_command_line(sys.argv)
>>>   File 
>>> "C:\Users\ZAB-SAKI\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py",
>>>  line 381, in execute_from_command_line
>>>     utility.execute()
>>>   File 
>>> "C:\Users\ZAB-SAKI\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py",
>>>  line 375, in execute
>>>     self.fetch_command(subcommand).run_from_argv(self.argv)
>>>   File 
>>> "C:\Users\ZAB-SAKI\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py",
>>>  line 323, in run_from_argv
>>>     self.execute(*args, **cmd_options)
>>>   File 
>>> "C:\Users\ZAB-SAKI\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py",
>>>  line 364, in execute
>>>     output = self.handle(*args, **options)
>>>   File 
>>> "C:\Users\ZAB-SAKI\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py",
>>>  line 83, in wrapped
>>>     res = handle_func(*args, **kwargs)
>>>   File 
>>> "C:\Users\ZAB-SAKI\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\commands\migrate.py",
>>>  line 234, in handle
>>>     fake_initial=fake_initial,
>>>   File 
>>> "C:\Users\ZAB-SAKI\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py",
>>>  line 117, in migrate
>>>     state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, 
>>> fake_initial=fake_initial)
>>>   File 
>>> "C:\Users\ZAB-SAKI\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py",
>>>  line 147, in _migrate_all_forwards
>>>     state = self.apply_migration(state, migration, fake=fake, 
>>> fake_initial=fake_initial)
>>>   File 
>>> "C:\Users\ZAB-SAKI\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py",
>>>  line 245, in apply_migration
>>>     state = migration.apply(state, schema_editor)
>>>   File 
>>> "C:\Users\ZAB-SAKI\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\migration.py",
>>>  line 124, in apply
>>>     operation.database_forwards(self.app_label, schema_editor, old_state, 
>>> project_state)
>>>   File 
>>> "C:\Users\ZAB-SAKI\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\operations\fields.py",
>>>  line 249, in database_forwards
>>>     schema_editor.alter_field(from_model, from_field, to_field)
>>>   File 
>>> "C:\Users\ZAB-SAKI\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\schema.py",
>>>  line 507, in alter_field
>>>     new_db_params = new_field.db_parameters(connection=self.connection)
>>>   File 
>>> "C:\Users\ZAB-SAKI\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\fields\related.py",
>>>  line 966, in db_parameters
>>>     return {"type": self.db_type(connection), "check": 
>>> self.db_check(connection)}
>>>   File 
>>> "C:\Users\ZAB-SAKI\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\fields\related.py",
>>>  line 963, in db_type
>>>     return self.target_field.rel_db_type(connection=connection)
>>>   File 
>>> "C:\Users\ZAB-SAKI\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\fields\related.py",
>>>  line 878, in target_field
>>>     return self.foreign_related_fields[0]
>>> IndexError: tuple index out of range
>>>
>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/7f038f19-7430-4d4b-8dc3-efad352d7bc8%40googlegroups.com
>>> <https://groups.google.com/d/msgid/django-users/7f038f19-7430-4d4b-8dc3-efad352d7bc8%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/2c8f88c6-5021-4113-84ee-ac0d53001e1a%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/2c8f88c6-5021-4113-84ee-ac0d53001e1a%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAP5HUWriqYDtP0p4RCTa_4Uk%3D0ERbg%2BmZ6xG0pT5S-8izA05FQ%40mail.gmail.com.

Reply via email to