Hello,
This error is raised when I run a migration allowing me to fill in the bd.
To try to resolve the error, I formatted the User object as a *SimpleLazyObject
*because I found that this is the type of object returned by the
*self.request.user* instance in a view.
Here is the model from which I fill the bd and the migration.

Thank you in advance for your solution

======================================================
my model

# Create your models here.

# Définition de notre classe BasicClass, précisement un model
class BasicClass(models.Model):

"""Model définissant les propriétés que toutes les classes modèles
partagent qui sont:
    - l'identifiant (attribut: id (UUIDField))
    - le slug (attribut: slug (SlugField))
    - la date de création (attribut: created_at  (DateTimeField))
    - la date de la dernière mise à jour (attribut: update_at  (DateTimeField))
    - le libellé (attribut: label (CharField: max_length=100 ))"""

    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=
False, verbose_name='ID')
    slug = models.SlugField(max_length=100)
    created_at = models.DateTimeField(auto_now_add=True, verbose_name=
"Date de création")
    update_at = models.DateTimeField(auto_now=True, verbose_name=
"Date de dernière modification")

    class Meta:
        abstract = True
        ordering = ['created_at']

# Définition de notre classe Restraint, précisement un model
class Restraint(BasicClass):
    """Model définissant une responsabilté caractérisé par:
    - son code (attribut: code (CharField: max_length=100))
    - son libellé (attribut: label (CharField: max_length=100))
    - sa prime en pourcentage ou non (attribut: percentage_bonus
(CharField: choices=RESPONSABILITY_BONUS_CHOICES, max_length=50))
    - sa valeur en pourcentage ou en montant (attribut:
value_amount_or_percentage (DecimalField: max_digits=6,
decimal_places=2))"""

    user = models.ForeignKey('auth.User', related_name='company_restraints'
, on_delete=models.PROTECT)
    code = models.CharField(max_length=100, unique=True, verbose_name="Code"
)
    label = models.CharField(max_length=100, verbose_name="Libellé")
    description = models.TextField(max_length=200, verbose_name=
"Description")
    employees = models.ManyToManyField('Employee', through=
'EmployeeRestraint', related_name="restrains")

    class Meta(BasicClass.Meta):
        verbose_name_plural = "Eléments de retenue"

======================================================
my migration

# Generated by Django 3.0.7 on 2020-12-08 18:38

from django.db import migrations, transaction
from django.contrib.auth.models import User
from django.contrib.auth import authenticate
from django.utils.functional import SimpleLazyObject

def create_salary_deductions(apps, schema_editor):
    # We can't import the Person model directly as it may be a newer
    # version than this migration expects. We use the historical version.
    with transaction.atomic():
        Restraint = apps.get_model('company', 'Restraint')
        try:
            user_instance = User.objects.get(username='ksm_system')
        except User.DoesNotExist:
            user_instance = User.objects.create_superuser(username=
'ksm_system', email= 'ksm_sys...@yowyob.com', password='w1c0net@app#deploy')
        user = user_instance
        Restraint.objects.create(user=user, code='ABNOJUST', label=
'Absence non justifiée', description=
"Au cours du mois, le salarié peut être volontairement ou
involontairement absent de son poste de travail"
)
        Restraint.objects.create(user=user, code='CAC', label=
'Centimes additionnels communaux', description=
"centimes additionnels communaux")
        Restraint.objects.create(user=user, code='IRPP', label=
'Impôt sur le revenu des personnes physique', description=
"l’impôt sur le revenu des personnes physique")
        Restraint.objects.create(user=user, code='RAV', label=
'Redevance audio-visuelle', description="la redevance audio-visuelle")
        Restraint.objects.create(user=user, code='PV', label=
'Pension vieillesse', description="Pension vieillesse")
        Restraint.objects.create(user=user, code='ACSA', label=
'Acomptes sur salaire', description="Acomptes sur salaire")
        Restraint.objects.create(user=user, code='PARAE', label=
'Prêts à rembourser à l’entreprise', description=
'Prêts à rembourser à l’entreprise')
        Restraint.objects.create(user=user, code='OSSSA', label=
'Opposition sur salaire, saisie-arrêt', description=
"Opposition sur salaire, saisie-arrêt")

class Migration(migrations.Migration):

    dependencies = [
        ('company', '0004_auto_20201208_1932'),
    ]

    operations = [
        migrations.RunPython(create_salary_deductions),
    ]

-- 
*Anselme Gildas TCHASSEM BOUTCHOUANG*
DevOps, Fullstack, Freelance
(+237) 696 319 191 / (+237) 698 544 992 / Email : *aansegild...@yahoo.fr
<aansegild...@yahoo.fr>* / LinkedIN : Profil LinkedIN
<http://www.linkedin.com/in/anselme-gildas-tchassem-boutchouang-2048a916b>
/ Facebook : Profil Facebook
<https://web.facebook.com/profile.php?id=100009069093492>  / Whatsapp:
(+237) 696 319 191

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAPB6euidKmHxHjJFSrPNPZk_JuXs%3D2K7AmNBspcDLvzWFfYFDw%40mail.gmail.com.

Reply via email to