я запускаю код makemigration и получаю много ошибок - PullRequest
0 голосов
/ 27 мая 2020

Я новичок в django Я хочу создать приложение business_plan На этапе создания моих моделей у меня есть много классов в моих моделях. Py:

from django.db import models
from django.contrib.auth.models import *

class CustomUser(AbstractUser):
    user_type=((1,"admin"),(2,"staff"))
    user_type=models.CharField(default=1,choices=user_type,max_length=10)


class Entrepreneur(models.Model):
    id_models= models.IntegerField(primary_key=True)
    admin=models.OneToOneField(CustomUser,on_delete=models.CASCADE())
    nom_et_prenom=models.CharField(max_length=50)
    date_naissance=models.DateField()
    adresse_entr=models.CharField(max_length=20)
    telephone=models.IntegerField()
    statut_social=(('ce','celébataire'),
               ('ma','marié'),
               ('di','divorcé'),
               ('ve','veuf'),
               )
    occupation=models.CharField(max_length=50)
    niveau_scolaire=(
        ('pri','primaire'),
        ('sec','secondaire'),
        ('cap','certificat aptitude professionel'),
        ('btp','brevet technicien professionel'),
        ('bts','brevet technicien superieur'),
        ('lic','license'),
        ('mai','maitrise'),
        ('mas','mastere'),
        ('doc','doctorat'),
                )
    niveau_scolaire=models.CharField(default='ser',choices=niveau_scolaire,max_length=50)
    annnee_exp=models.IntegerField()
    email=models.CharField(max_length=255)
    password=models.CharField(max_length=255)

class Projet(models.Model):
    id_models=models.IntegerField(primary_key=True)
    admin = models.OneToOneField(CustomUser, on_delete=models.CASCADE())
    nom_projet=models.CharField(max_length=30)
    lieu_implantation=models.CharField(max_length=20)
    type_projet=(
        ('ser','service'),
        ('com','commerce'),
        ('agr','agricuture'),
        ('ind','industrie'),
        ('IT','technologie information'),
        ('art','artisanat'),
        ('tor','tourisme'),
    )
    type_projet=models.CharField(default='ser',choices=type_projet,max_length=50)
    produit=models.CharField(max_length=50)
    id_promoteur=models.ForeignKey(Entrepreneur,on_delete=models.CASCADE)

class Admin(models.Model):
    id_admin=models.AutoField(primary_key=True)
    name=models.CharField(max_length=255)
    email=models.CharField(max_length=255)
    password=models.CharField(max_length=255)
    objects=models.Manager()


class CustomUser(AbstractUser):
    user_type=((1,"admin"),(2,"staff"))
    user_type=models.CharField(default=1,choices=user_type,max_length=10)

это мои файлы manage.py:

#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os 
import sys


def main():
   os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'oasisconsuting.settings')
   try:
    from django.core.management import execute_from_command_line
   except ImportError as exc:
    raise ImportError(
        "Couldn't import Django. Are you sure it's installed and "
        "available on your PYTHONPATH environment variable? Did you "
        "forget to activate a virtual environment?"
    ) from exc
   execute_from_command_line(sys.argv)
 if __name__ == '__main__':
    main()

когда я запускаю makemigration после создания моих классов, у меня возникает эта ошибка:

 > (business) C:\Users\hp\PycharmProjects\business\oasisconsuting>python manage.py makemigrations
 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\hp\PycharmProjects\business\lib\site-packages\django\core\management\__init__.py", 
 line 401, in execute_from_command_line
 utility.execute()
 File "C:\Users\hp\PycharmProjects\business\lib\site-packages\django\core\management\__init__.py", 
 line 377, in execute
 django.setup()
 File "C:\Users\hp\PycharmProjects\business\lib\site-packages\django\__init__.py", line 24, in setup
 apps.populate(settings.INSTALLED_APPS)
 File "C:\Users\hp\PycharmProjects\business\lib\site-packages\django\apps\registry.py", line 114, in 
 populate
   app_config.import_models()
 File "C:\Users\hp\PycharmProjects\business\lib\site-packages\django\apps\config.py", line 211, in 
 import_models
 self.models_module = import_module(models_module_name)
 File "C:\Users\hp\PycharmProjects\business\lib\importlib\__init__.py", line 127, in import_module
 return _bootstrap._gcd_import(name[level:], package, level)
 File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
 File "<frozen importlib._bootstrap>", line 991, in _find_and_load
 File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
 File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
 File "<frozen importlib._bootstrap_external>", line 783, in exec_module
 File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
 File "C:\Users\hp\PycharmProjects\business\oasisconsuting\businessplan\models.py", line 9, in 
 <module>
 class Entrepreneur(models.Model):
 File "C:\Users\hp\PycharmProjects\business\oasisconsuting\businessplan\models.py", line 11, in 
 Entrepreneur
 admin=models.OneToOneField(CustomUser,on_delete=models.CASCADE())
 TypeError: CASCADE() missing 4 required positional arguments: 'collector', 'field', 'sub_objs', and 
 'using'

где мои ошибки, вы можете помочь исправить эти ошибки Я использую базу данных sqite3

Ответы [ 3 ]

1 голос
/ 27 мая 2020

Просто удалите скобки в КАСКАДЕ, так как это не метод, а константа

class Entrepreneur(models.Model):
  admin=models.OneToOneField(CustomUser,on_delete=models.CASCADE)

class Projet(models.Model):
  id_promoteur=models.ForeignKey(Entrepreneur,on_delete=models.CASCADE)
0 голосов
/ 27 мая 2020

Причина в том, что вы вызываете models.CASCADE со скобками после него. Последняя строка в вашей трассировке показывает, что требуется 4 аргумента, потому что вы вызываете эту функцию, а не просто ссылаетесь на нее.

Требуемая строка:

...

class Entrepreneur(models.Model):
    id_models= models.IntegerField(primary_key=True)
    admin=models.OneToOneField(CustomUser,on_delete=models.CASCADE)
...
0 голосов
/ 27 мая 2020

спасибо, я исправил ошибки, связанные с баркетами в моделях. КАСКАД

, но когда я повторно запускаю код, он показывает другие ошибки: (я не знаю, ошибка это или предупреждения

> C:\Users\hp\PycharmProjects\business\lib\site-packages\django\db\models\base.py:320: 
RuntimeWarning: Model 'businessplan.customuser' was already registered. Reloadin
g models is not advised as it can lead to inconsistencies, most notably with related 
models.
  new_class._meta.apps.register_model(new_class._meta.app_label, new_class)
SystemCheckError: System check identified some issues:

ERRORS:
auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with 
reverse accessor for 'CustomUser.groups'.
    HINT: Add or change a related_name argument to the definition for 'User.groups' or 
'CustomUser.groups'.
auth.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permissions' 
clashes with reverse accessor for 'CustomUser.user_permissions'.
    HINT: Add or change a related_name argument to the definition for 
'User.user_permissions' or 'CustomUser.user_permissions'.
businessplan.CustomUser.groups: (fields.E304) Reverse accessor for 'CustomUser.groups' 
clashes with reverse accessor for 'User.groups'.
    HINT: Add or change a related_name argument to the definition for 
'CustomUser.groups' or 'User.groups'.
businessplan.CustomUser.user_permissions: (fields.E304) Reverse accessor for 
'CustomUser.user_permissions' clashes with reverse accessor for 
'User.user_permissions'.

    HINT: Add or change a related_name argument to the definition for 
'CustomUser.user_permissions' or 'User.user_permissions'.
businessplan.CustomUser_groups: (fields.E336) The model is used as an intermediate 
model by 'businessplan.CustomUser.groups', but it does not have a foreign key to '
CustomUser' or 'Group'.
businessplan.CustomUser_user_permissions: (fields.E336) The model is used as an 
intermediate model by 'businessplan.CustomUser.user_permissions', but it does not hav
e a foreign key to 'CustomUser' or 'Permission'.
businessplan.Entrepreneur.admin: (fields.E300) Field defines a relation with model 
  'CustomUser', which is either not installed, or is abstract. 
businessplan.Projet.admin: (fields.E300) Field defines a relation with model 
  'CustomUser', 
   which is either not installed, or is abstract.
...