Re: ValueError: Cannot assign "<User: ksm_system>": "Restraint.user" must be a "User" instance.

33 views
Skip to first unread message

Anselme Gildas Tchassem Boutchouang

unread,
Dec 12, 2020, 11:11:06 AM12/12/20
to django...@googlegroups.com
Thank you in advance for your solution 

Le sam. 12 déc. 2020 à 07:51, Anselme Gildas Tchassem Boutchouang <tbag...@gmail.com> a écrit :
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.



--
Anselme Gildas TCHASSEM BOUTCHOUANG
DevOps, Fullstack, Freelance
(+237) 696 319 191 / (+237) 698 544 992 / Email : aanseg...@yahoo.fr / LinkedIN : Profil LinkedIN / Facebook : Profil Facebook  / Whatsapp: (+237) 696 319 191


--
Anselme Gildas TCHASSEM BOUTCHOUANG
DevOps, Fullstack, Freelance
(+237) 696 319 191 / (+237) 698 544 992 / Email : aanseg...@yahoo.fr / LinkedIN : Profil LinkedIN / Facebook : Profil Facebook  / Whatsapp: (+237) 696 319 191

Anselme Gildas Tchassem Boutchouang

unread,
Dec 12, 2020, 11:11:14 AM12/12/20
to django...@googlegroups.com
Restrain.py
0005_salary_deductions.py

Anselme Gildas Tchassem Boutchouang

unread,
Dec 13, 2020, 7:35:12 PM12/13/20
to django...@googlegroups.com
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=Truedefault=uuid.uuid4, editable=Falseverbose_name='ID')
    slug = models.SlugField(max_length=100)
    created_at = models.DateTimeField(auto_now_add=Trueverbose_name="Date de création")
    update_at = models.DateTimeField(auto_now=Trueverbose_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=100unique=Trueverbose_name="Code")
    label = models.CharField(max_length=100verbose_name="Libellé")
    description = models.TextField(max_length=200verbose_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(appsschema_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_s...@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),
    ]
Reply all
Reply to author
Forward
0 new messages