Answer the question
In order to leave comments, you need to log in
How to work with models in django?
Hello!
In my project with a structure:
There is this code in the userroles/models.py folder:
from django.db import models
from django.db.models.signals import post_save
from django.contrib.auth.models import User, Group
from distrib_system.choose_distrib.models import Container
class UserProfile(models.Model):
user = models.OneToOneField(User)
containers = models.ManyToManyField(Container)
class StudentManager(models.Manager):
def get_query_set(self):
return super(StudentManager, self).get_query_set().filter(student__enrolled=True).distinct()
class Student(Group):
group = models.CharField(max_length = 10)
objects = models.Manager()
has_students = StudentManager()
class Meta:
verbose_name_plural = "Students"
ordering = ['name']
permissions = {}
class CooperatorManager(models.Manager):
def get_query_set(self):
return super(CooperatorManager, self).get_query_set().filter(cooperator__enrolled=True).distinct()
class Cooperator(Group):
work = models.CharField(max_length = 100)
objects = models.Manager()
has_students = CooperatorManager()
class Meta:
verbose_name_plural = "Cooperators"
ordering = ['name']
permissions = {}
class ProfessorManager(models.Manager):
def get_query_set(self):
return super(ProfessorManager, self).get_query_set().filter(professor__enrolled=True).distinct()
class Professor(Group):
education_course = models.CharField(max_length = 100)
objects = models.Manager()
has_students = ProfessorManager()
class Meta:
verbose_name_plural = "Professors"
ordering = ['name']
permissions = {}
class ScientificDirectorManager(models.Manager):
def get_query_set(self):
return super(ScientificDirectorManager, self).get_query_set().filter(scientificdirector__enrolled=True).distinct()
class ScientificDirector(Group):
education_course = models.CharField(max_length = 100)
objects = models.Manager()
has_students = ScientificDirectorManager()
class Meta:
verbose_name_plural = "Scientific directors"
ordering = ['name']
permissions = {}
def create_profile(sender, **kwargs):
user = kwargs["instance"]
if kwargs["created"]:
user_profile = UserProfile(user=user)
user_profile.save()
post_save.connect(create_profile, sender=User)
from django.db import models
from django.core.validators import MaxValueValidator, MinValueValidator
from distrib_system.userroles.models import UserProfile
# Create your models here.
class Container(models.Model):
#лаборатория
container_name = models.CharField(max_length = 100, verbose_name = 'Название лаборатории')
container_director = models.OneToOneField(UserProfile, on_delete= models.CASCADE)
container_capacity = models.IntegerField(
default = 0,
validators=[MaxValueValidator(200), MinValueValidator(0)]
)
def __str__(self):
return "Название лаборатории: {0}".format(self.lab_name)
class Request(models.Model):
#Запрос в "контейнер"
student = models.ForeignKey(UserProfile, on_delete= models.CASCADE)
container = models.ForeignKey(Container, on_delete= models.CASCADE)
'''
0 = SENDED
1 = ACCEPTED
2 = DECLINED
'''
status = models.IntegerField(default = 0)
send_date = models.DateField(auto_now=True)
D:\LiClipse\Projects\Система распределения\distrib_system>python manage.py makemigrations
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "D:\Python36\lib\site-packages\django\core\management\__init__.py", line 363, in execute_from_command_line
utility.execute()
File "D:\Python36\lib\site-packages\django\core\management\__init__.py", line 337, in execute
django.setup()
File "D:\Python36\lib\site-packages\django\__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "D:\Python36\lib\site-packages\django\apps\registry.py", line 108, in populate
app_config.import_models()
File "D:\Python36\lib\site-packages\django\apps\config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "D:\Python36\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "D:\LiClipse\Projects\Система распределения\distrib_system\choose_distrib\models.py", line 3, in <module>
from distrib_system.userroles.models import UserProfile
ModuleNotFoundError: No module named 'distrib_system.userroles'
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question