AttributeError: 'module' object has no attribute 'iglob' - python

Strange error. I'm just trying to get the models right in my DB, but fail at the very first. Here is my glob.models:
from django.db import models
class Status(models.Model):
status_name = models.CharField(max_length=50)
status_description = models.CharField(max_length=100, blank=True, null=True)
def __str__(self):
return self.status_name
When I try to run syncd, I get:
AttributeError: 'module' object has no attribute 'iglob'

Your models are in glob.models - I assume the app name is glob. Is it possible that you may have made a typo somewhere - iglob instead of glob?
The code you pasted has no mention of the word iglob. I suggest you do a project wide search for iglob and see if it's meant to be glob.

Maybe your variable name is almost the same as the imported module name. You can try to use import some_module as name to avoid this issue.

Related

ImportError: cannot import name 'Maca' from partially initialized module 'maca.models' (most likely due to a circular import)

I have this error ImportError: cannot import name 'Maca' from partially initialized module 'maca.models' (most likely due to a circular import).
I have code like this
from maca.models import Maca
class Maca2(models.Model)
maca = models.ForeignKey(
Maca, on_delete=models.CASCADE
)
Now to model "Maca" I'm trying to access every single "Maca2" objects like this
from maca2.models import Maca2
class Maca(models.Model)
...
#property
maca_has_maca2(self)
maca2 = Maca2.objects.filter(maca=self.id)
Can you help me to handle this?
You can import the Maca2 in the maca_has_maca2 property:
# no import of maca2.models
class Maca(models.Model):
# …
#property
def maca_has_maca2(self):
from maca2.models import Maca2
maca2 = Maca2.objects.all()
For ForeignKeys, OneToOneFields and ManyToManyFields, you can make use of a string literal with as structure 'app_name.ModelName' to refer to a model, for example:
# no import of maca2.models
class Maca(models.Model):
maca2 = models.ForeignKey(
'maca2.Maca2', on_delete=models.CASCADE
)
This avoids importing modules and thus circular imports. If the model has the same app_name, you can reference this by 'ModelName'.

AttributeError: module has no attribute

When I try to change function name "random_string" which is used in auth_code (variable in model class) to any other name it shows me the error in the command line: AttributeError: module 'users_data.models' has no attribute 'random_string'
from django.db import models
from django.utils.timezone import now
import random
from django.core.exceptions import ValidationError
def random_string():
return int(random.randint(000000, 999999))
def validate_phone_number(phone):
if len(phone) < 7:
raise ValidationError('Phone number can not be less than 7 digits')
class Users(models.Model):
phone = models.CharField(verbose_name='Phone', max_length=20, validators=
[validate_phone_number])
auth_code = models.IntegerField(verbose_name='Code',
default=random_string)
get_in_date = models.DateTimeField(default=now, blank=False,
editable=False)
I have seen many posts which cover my problem but I didn't find any useful. I would appreciate any help.
The issue lies within your migrations files, as shown in this issue. Basically, when you generated your previous migration files, it was written that the default was value for a field was random_string.
Now, if you change that function name, your current code will work, but because your already-generated migration files use this function, they will raise an error as they cannot find that function anymore.
I dont know if simply updating the files to replace the name within them would be enough. Other solutions would be to reset the migrations (though it might come as a cost).
The link I've provided offers a script to fix that, but I haven't tested it myself

AttributeError: module 'django.db.models' has no attribute 'Models'

When I'm trying to migrate a new app onto the server i get this error
AttributeError: module 'django.db.models' has no attribute 'Models'- in terminal
I'm using PyCharm. I am very fresh to Django and web development so any tips will help. Thanks!
from django.db import models
# Create your models here.
class product(models.Model):
item = models.Textfiels()
description = models.Textfields()
price = models.Textfields()
There's no such class django.db.models.TextFields but this works for me on any recent version :
from django.db import models
class product(models.Model):
item = models.TextFiel()
description = models.TextField()
price = models.TextField()
You made 2 typos : the correct name is TextField and you typed Textfields (Python is case sensitive)
I suspect you didn't setup correctly your project under PyCharm. When correctly setup, it shows warnings on misspelled names (names underlined with red dots with default setting).
There's another variation to this question and that is in the form of:
AttributeError: module 'django.contrib.auth' has no attribute 'models'
As far as I can tell this is typically caused by conflicting imports or improperly imported files. Another cause could be changes to Django's updates but I'm not sure about that as I didn't find any documentation that changed that aspect of the Django library.
Short term solution to this is as follows:
from django.contrib.auth import models
class MyClass(models.User): """ """
This will allow you to at least test your runserver command and website on a browser of your choosing.
I'm still trying to figure out any other solutions to this problem that can be a fix for individually importing the 'auth' module itself.
At the time of this writing I'm using Django 2.2.6 whereas Django 2.2.7 is out and 2.2.8 is on the way to be released.
I'm not sure if this is the solution , but when I had this problem it was because in my admin.py file I had
from django.contrib import admin
from meetings.models import Meeting, Room
admin.site.register(Meeting, Room)
But changing it to solved the issue
from django.contrib import admin
# Register your models here.
from meetings.models import Meeting, Room
admin.site.register(Meeting)
admin.site.register(Room)

Django FileField upload_to assistance

I'm new to python and trying to adapt to the OOP of Python. Could someone explain why the following is saving in a folder called 'None'? I want to upload an audio file in the admin page. This file gets stored in its own folder with the 'Vocab name'
class Vocab(models.Model):
module = models.ForeignKey(Modules, on_delete=models.CASCADE)
number = models.CharField(max_length = 250)
name = models.CharField(max_length = 250)
class VocabContent(models.Model):
vocab = models.ForeignKey(Vocab, on_delete=models.CASCADE)
audio = models.FileField(upload_to=vocab.name)
Running the following on shell.
>>> from module.models import Modules, Vocab, VocabContent
>>> vocab = VocabContent.objects.get(pk=1)
>>> vocab.vocab.name
'Numbers'
Numbers is the value i am looking for.
It's probably because the way you reference vocab.name is not defined when your model migration is run. I can't explain precisely why this happens but a solution would be to use a callable as your upload_to to evaluate it at runtime and get the value correctly, much like this other answer: Dynamic File Path in Django
So, for you, you could have something like:
import os
def get_upload_path(instance, filename):
return os.path.join("%s" % instance.vocab.name, filename)
class VocabContent(models.Model):
vocab = models.ForeignKey(Vocab, on_delete=models.CASCADE)
audio = models.FileField(upload_to=get_upload_path) # Important to NOT put the parenthesis after the function name
Which would result in a path that concatenates the vocab.name to your file name for every new file.

Django: How can I get the foreign key's class from a classmethod in the referred-to class?

I have the following two Django Classes MyClassA and MyClassB in two separate files. MyClassB has a foreign key reference to an instance of MyClassA. MyClassA cannot import the class MyClassB.
my_class_a/models.py:
from django.db import models
class MyClassA(models.Model):
name = models.CharField(max_length=50, null=False)
#classmethod
def my_method_a(cls):
# What do I put here to call MyClassB.my_method_b()??
my_class_b/models.py:
from my_class_a.models import MyClassA
from django.db import models
class MyClassB(models.Model):
name = models.CharField(max_length=50, null=False)
my_class_a = models.ForeignKey(MyClassA, related_name="MyClassB_my_class_a")
#staticmethod
def my_method_b():
return "Hello"
From within MyClassA's class method my_method_a, I would like to call MyClassB's static method my_method_b. How can I do it?
If my_method_a was an instance method, I would simply do self.MyClassB_my_class_a.model.my_method_b(). But since I don't have an instance of MyClassA, I don't know how to do it. I would like to take advantage of the related_name field that allows for reverse lookups of instances.
You can do it like this.
#classmethod
def my_method_a(cls):
from myclass_b.models import MyClassB
# yes, you can have an import here. and it will not
# lead to a cyclic import error
MyClassB.my_method_b()
The import failure happens only if you add the import to the top of the file. That would lead to cyclic imports one module cannot be loaded because it depends on another which depends on the other module. However when the import is inside a method the same problem does not arise.

Categories