It's called models.Model and not models.model (case sensitive). Fix your Poll model like this -
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
Answer from Baishampayan Ghose on Stack OverflowIt's called models.Model and not models.model (case sensitive). Fix your Poll model like this -
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
I also got the same error but I noticed that I had typed in Foreign*k*ey and not Foreign*K*ey,(capital K) if there is a newbie out there, check out spelling and caps.
class - Python: instance has no attribute - Stack Overflow
AttributeError: module 'openai.api_resources' has no attribute 'Model'
model.fit : AttributeError: 'Model' object has no attribute '_compile_metrics'
AttributeError: 'module' object has no attribute 'Model'
Videos
Your class doesn't have a __init__(), so by the time it's instantiated, the attribute atoms is not present. You'd have to do C.setdata('something') so C.atoms becomes available.
>>> C = Residues()
>>> C.atoms.append('thing')
Traceback (most recent call last):
File "<pyshell#84>", line 1, in <module>
B.atoms.append('thing')
AttributeError: Residues instance has no attribute 'atoms'
>>> C.setdata('something')
>>> C.atoms.append('thing') # now it works
>>>
Unlike in languages like Java, where you know at compile time what attributes/member variables an object will have, in Python you can dynamically add attributes at runtime. This also implies instances of the same class can have different attributes.
To ensure you'll always have (unless you mess with it down the line, then it's your own fault) an atoms list you could add a constructor:
def __init__(self):
self.atoms = []
the error means the class Residues doesn't have a function call atoms. The solution could be as follows:
class Residues:
def setdata(self, atoms, name=None):
self.name = name
self.atoms =[]
C = Residues()
C.setdata(atoms= " a ")
TLDR: add use_in_migrations = True in your custom managers if you want to do query in migration files
After a few hour of searching i came across this part in the document about model manager and migration
https://docs.djangoproject.com/en/3.2/topics/migrations/#model-managers
For having custom managers in migration, use_in_migrations = True need to be set in the managers so i change my managers to the following:
class StudentManager(models.Manager):
use_in_migrations = True
def student_query(self, student):
return super(StudentManager, self).get_queryset().filter(
Q(students=student) | Q(course__students=student
)).distinct('id')
class TeacherManager(models.Manager):
use_in_migrations = True
def teacher_query(self, teacher):
return super(TeacherManager, self).get_queryset().filter(
Q(teacher=teacher) | Q(course__teacher=teacher)|
Q(substitute_teacher=teacher)).distinct('id')
class MainCourseClassManager(models.Manager):
use_in_migrations = True
class CourseClass(models.Model):
class Meta:
db_table = 'classes'
verbose_name = _('Class')
verbose_name_plural = _('Classes')
objects = MainCourseClassManager()
student_manager = StudentManager()
teacher_manager = TeacherManager()
And now my migrations work with get_model()
Also noted from the docs
Because it’s impossible to serialize arbitrary Python code, these historical models will not have any custom methods that you have defined. They will, however, have the same fields, relationships, managers (limited to those with use_in_migrations = True) and Meta options (also versioned, so they may be different from your current ones).
I have encountered similar problems during custom migrations. I think one of the reasons is the class returned by get_model is sometimes not the full-fledged model class you would get from importing it properly. The get_model call, however, is necessary to make sure the model is properly loaded for the time of the migration. That is because at migration time, the model is supposed to represent the model in its state after the previous migration. I fthe manager was defined at a later time, it will not be there yetOne workaround that works for us:
def update_student(apps, schema_editor):
_ = apps.get_model('backend', 'CourseClass')
# makes sure model is loaded
from path.to.backend.models import CourseClass
# gives you the proper class with all utils
# do your thang
Your indentation is goofed, and you've mixed tabs and spaces. Run the script with python -tt to verify.
If you’re using python 3+ this may also occur if you’re using private variables that start with double underscore, e.g., self.__yourvariable. Just something to take note of for some of you who may run into this issue.
You have mutual top-level imports, which is almost always a bad idea.
If you really must have mutual imports in Python, the way to do it is to import them within a function:
# In b.py:
def cause_a_to_do_something():
import a
a.do_something()
Now a.py can safely do import b without causing problems.
(At first glance it might appear that cause_a_to_do_something() would be hugely inefficient because it does an import every time you call it, but in fact the import work only gets done the first time. The second and subsequent times you import a module, it's a quick operation.)
I have also seen this error when inadvertently naming a module with the same name as one of the standard Python modules. E.g. I had a module called commands which is also a Python library module. This proved to be difficult to track down as it worked correctly on my local development environment but failed with the specified error when running on Google App Engine.