Tuesday, November 26, 2019

Re: self.foreign_related_fields[0] IndexError: tuple index out of range

Hi,

If you have deleted your old db and creating a fresh database, then delete all files generated previously using migration command and do migration

(have a backup before deleting files) ;)

On Tue, 26 Nov 2019 at 23:57, Ahmad Saki <saki.programming@gmail.com> wrote:
Hi friend,

When I want to create my custom authentication model User. I have got error such as 'FieldDoesNotExist 'userid''.
So, I have deleted all of these previous migration_files, re-create database and makemigrations. But cannot able to migrate again.


On Wednesday, November 27, 2019 at 12:05:24 AM UTC+6, Integr@te System wrote:
Hi friend,

plz check these files: '  # Generated by Django 2.2.3 on 2019-11-26 15:36 ' and another (if u have), to see separating.

On Tue, Nov 26, 2019, 23:44 Ahmad Saki <saki.pr...@gmail.com> wrote:
Hello Dear,
Please explain with more words. How do I solve this problem? What can I do?

On Tuesday, November 26, 2019 at 9:54:26 PM UTC+6, Integr@te System wrote:
Hi man,

And u see error occur again, isn't it?
Do you differ two file that app output when you migrated?

On Tue, Nov 26, 2019, 22:40 Ahmad Saki <saki.pr...@gmail.com> wrote:
organization.Organization model:

from django.db import models

class Organization(models.Model):
   orgid = models.IntegerField(max_length=6, primary_key=True)
   name = models.CharField(max_length=50)
   phone = models.CharField(max_length=20,null=True,blank=True,default=None)
   email = models.CharField(max_length=50,null=True,blank=True,default=None)
   mobile = models.CharField(max_length=20,null=True,blank=True,default=None)
   permanent_address = models.CharField(max_length=200,null=True,blank=True,default=None)
   present_address = models.CharField(max_length=200,null=True,blank=True,default=None)
   remark = models.CharField(max_length=500,null=True,blank=True,default=None)

And migration:

# Generated by Django 2.2.3 on 2019-11-26 15:36

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
   ]

    operations = [
       migrations.CreateModel(
           name='Organization',
           fields=[
               ('orgid', models.IntegerField(max_length=6, primary_key=True, serialize=False)),
               ('name', models.CharField(max_length=50)),
               ('phone', models.CharField(blank=True, default=None, max_length=20, null=True)),
               ('email', models.CharField(blank=True, default=None, max_length=50, null=True)),
               ('mobile', models.CharField(blank=True, default=None, max_length=20, null=True)),
               ('permanent_address', models.CharField(blank=True, default=None, max_length=200, null=True)),
               ('present_address', models.CharField(blank=True, default=None, max_length=200, null=True)),
               ('remark', models.CharField(blank=True, default=None, max_length=500, null=True)),
           ],
       ),
   ]




On Tuesday, November 26, 2019 at 8:26:13 PM UTC+6, Integr@te System wrote:
hi, inspect your typo and Organization model.

On Tue, Nov 26, 2019, 16:31 Ahmad Saki <saki.pr...@gmail.com> 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 <saki.pr...@gmail.com> 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 django...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7f038f19-7430-4d4b-8dc3-efad352d7bc8%40googlegroups.com.

--
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 django...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2c8f88c6-5021-4113-84ee-ac0d53001e1a%40googlegroups.com.

--
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 django...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/84f02bae-7119-4066-a3d3-3dfc8b8c5973%40googlegroups.com.

--
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 django...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1c7a93a2-7eb8-4b9d-9928-aa14f86065a8%40googlegroups.com.

--
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 django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d0684d23-aa0a-4c98-b1cc-e0150ed22651%40googlegroups.com.


--
Thanks & Regards,

Raja Sekar Sampath
+91 9087434567.

--
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 django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAAS5AE6S%3D%2BxpExoR9GP%3D5frKKQ73LKBCUHVvdqTuiDSiTWzvtQ%40mail.gmail.com.

No comments:

Post a Comment