AttributeError: module 'django.db.models' has no attribute 'get_models' - python

Why am I getting an issue when calling the get_model() function? Here is what I am trying to do:
#classmethod
def get_content_models(cls):
"""
Return all Package subclasses.
"""
is_content_model = lambda m: m is not Package and issubclass(m, Package)
return list(filter(is_content_model, models.get_models()))
This used to work before, but now after updating to the new Django, it's throwing an error. How can this be resolved?
UPDATE
Below is my model
from django.db import models
class Package(BasePackage):
"""
A package in the package tree. This is the base class that custom content types
need to subclass.
"""
parent = models.ForeignKey("Package", blank=True, null=True, related_name="children", on_delete=models.CASCADE)
titles = models.CharField(editable=False, max_length=1000, null=True)
content_model = models.CharField(editable=False, max_length=50, null=True)
in_menus = MenusField(_("Show in menus"), blank=True, null=True)
login_required = models.BooleanField(_("Login required"), default=False,
help_text=_("If checked, only logged in users can view this Package"))
itinerary = models.ManyToManyField('ItineraryItem', through="PackageItinerary")
def __str__(self):
return self.title
def save(self, *args, **kwargs):
"""
Create the titles field using the titles up the parent chain
and set the initial value for ordering.
"""
if self.id is None:
self.content_model = self._meta.object_name.lower()
self.titles = self.title
super(Package, self).save(*args, **kwargs)
#classmethod
def get_content_models(cls):
"""
Return all Package subclasses.
"""
is_content_model = lambda m: m is not Package and issubclass(m, Package)
return list(filter(is_content_model, models.get_models()))
def get_content_model(self):
"""
Provies a generic method of retrieving the instance of the custom
content type's model for this Package.
"""
return getattr(self, self.content_model, None)

It is an AttributeError owing to the fact that models.get_model() was removed in Dango 1.9.
You are supposed to use dango.apps.apps.get_model().
Some discussion is here and here
Here is how you use it now.
from django.apps import apps
MyModel = apps.get_model('app name where the model is','name of the model you want to get from that app')
# Do your logic here with MyModel
However, if all you want is to get model, why not import it straight away? How you are using the code downstream? Please note that due to change (from 1.9 onwards) the properties of function might have changed. Thus you may want to consider latest module and functions to achieve your results (that you previously used to get). This means more work for you to come in sync with later versions of Django but you might run into problems anyways due to the change in get_model.
In summary, see what the code is doing and adapt to newer versions of Django.
I am not sure if I helped you or confused you. Sorry if I did the later.
Best wishes.

from django.apps import apps
ModelClass = apps.get_model('app_name.ModelClass')
You can now instatiate this class
mc = ModelClass()
Doc here

Related

Django cant figure out how to use foreign key

class Project_types(models.Model):
project_type = models.CharField(max_length=200)
def __str__(self):
return self.project_type
class Projects(models.Model):
project_types = models.ForeignKey(Project_types, on_delete=models.CASCADE)
project = models.CharField(max_length=200)
def __str__(self):
return self.project
When I try to run Project_types(project_type='games').item_set.all()
I get an error saying that there is no attribute item set.
class Project_types(models.Model):
project_type = models.CharField(max_length=200)
def __str__(self):
return self.project_type
class Projects(models.Model):
project_types = models.ForeignKey(Project_types, on_delete=models.CASCADE)
project = models.CharField(max_length=200)
def __str__(self):
return self.project
First there's a few problems with your model.
First model names shouldn't be pluralized
Here a Projects (should be Project) has one project_type, and a Project_types (should be ProjectType) has one project.
To run the query you want:
Project_types.filter(project_type='games').item_set.all()
the correct query would be:
Project_types.filter(project_type='games').projects_set.all()
use projects instead of items,
the related manager is based on the Model name (in this case Projects becomes projects_set)
see here https://docs.djangoproject.com/en/3.2/topics/db/examples/many_to_one/
The .item_set attribute does not exist on the instance you have created by running:
Project_types(project_type='games')
It seems to me that you are trying to get all Projects of the type 'games'.
To do that, you'll have to use the QuerySet of the Projects class like this:
Projects.objects.filter(project_types__project_type='games').all()
Additionally, a suggestion: try to name all your model classes using singular CamelCase, so that they will be easier to understand. In your example, Project_types should be ProjectType, while Projects should be Project.
Project_types(project_type='games') doesn't actually return any object. That's why you got that attribute error. You need to add a filter or use get. Something like below:
Project_types.objects.get(project_type='games').item_set.all()
Or
Project_types.objects.filter(project_type='games').item_set.all()

Reserved fields of Django model

I'd like to have a Django model with a reserved field, so that no one can set it directly but its value it's generated at saving time. This is useful for example to generate user tokens and I want to prevent developers to directly set a value for the token key. At the same time I would like to be able to treat that field as I do with others, so using __ for fields lookup in queries, or be able to retrieve tokens as:
token = Token.objects.get(key='c331054c00494f6a22f0ebde7a32bf9d4619b988')
So in my mind doing something like:
Token.key = 'my-token-key'
should fail, and even instantiation should fail:
token = Token(key='my-token-key')
So far I came up with this solution, but I'm a bit concerned my changes could break some Django workflow since I'm not sure what my changes will affect:
import binascii
import datetime
import os
from django.contrib.auth import get_user_model
from django.db import models
class Token(models.Model):
"""
An access token that is associated with a user.
"""
id = models.AutoField(primary_key=True)
# By default `get_attname` returns the field `name`,
# but in my case the attribute name is different
_key = models.CharField(max_length=40, unique=True, name='key', db_column='key')
_key.get_attname = lambda: '_key'
name = models.CharField(max_length=255)
user = models.ForeignKey(get_user_model(), related_name='tokens')
created = models.DateTimeField(auto_now_add=True)
last_access_time = models.DateTimeField(null=True, blank=True)
expires = models.DateField(
null=True,
blank=True,
help_text="Leave empty for non-expiring tokens. "
"Once the token has expired you can not extend its validity.",
)
#property
def key(self):
return self._key
#key.setter
def key(self, value):
raise ValueError("Can not set key directly. It is automatically generated when saving the model.")
def save(self, *args, **kwargs):
if not self._key:
self._key = self._generate_key()
super(Token, self).save(*args, **kwargs)
#staticmethod
def _generate_key():
return binascii.hexlify(os.urandom(20)).decode()
#property
def expired(self):
return bool(self.expires and self.expires < datetime.date.today())
def __str__(self):
return '{} - {}'.format(self.user, self.name)
class Meta:
verbose_name = "User Token"
verbose_name_plural = "User Tokens"
unique_together = (('name', 'user'),)
As you can see I tried overriding the get_attname method of the key field (needed because the field name and the property are the same and it would lead to errors loading forms). This seems to work just fine, but I would like to know if this could lead to problems running queries.
Maybe there is a simpler way to do this but I couldn't find anything better.
P.S.: I'm using python2 with Django 1.11
Thanks a lot to everyone!

django model instance method not being called

I want to update my model upon login (to check the authorizations of a person from an external system).
The code of my model looks as follow:
import json
from django.contrib.auth.models import AbstractUser
from django.contrib.auth.signals import user_logged_in
from django.db import models
class Person(AbstractUser):
is_dean = models.BooleanField(null=False, blank=False, default=False)
is_institute_manager = models.BooleanField(null=False, blank=False, default=False)
managed_institutes = models.TextField(blank=True, null=True, default=None)
def get_managed_institutes(self):
return json.loads(self.managed_institutes)
def set_managed_institutes(self, value):
self.managed_institutes = json.dumps(value)
# Signals processing
def check_authorizations(sender, user, request, **kwargs):
...
# check if the user is dean
is_dean = False
# logic to check if the user is dean...
user.is_dean = is_dean
# Check if the user manages institutes
is_institute_manager = False
managed_institutes = list()
# Logic to check if the user is managing institutes ...
user.is_institute_manager = is_institute_manager
user.set_managed_institutes = managed_institutes
user.save()
user_logged_in.connect(check_authorizations)
Surprisingly, the boolean flags get set correctly, but the method set_managed_institute never gets called...
I am quite convinced this a trivial mistake from my end, but I can't figure it out.
That is not how you call methods in Python. You need to do so explicitly:
user.set_managed_institutes(managed_institutes)
Or did you mean to define a property?
#property
def managed_institutes(self):
return json.loads(self._managed_institutes)
#managed_institutes.setter
def managed_institutes(self, value):
self._managed_institutes = json.dumps(value)
But also note, you probably want to use a JsonField anyway. If you're using PostgreSQL, there is one defined in Django directly; otherwise there are several third-party libraries that take care of serializing and deserializing your data on load/save.

Django: how to use a DB parameter in a default attribute of model field?

I have created a (kind of) singleton to put all the app parameters in my database:
class SingletonModel(models.Model):
def save(self, *args, **kwargs):
self.pk = 1
super(SingletonModel, self).save(*args, **kwargs)
#classmethod
def load(cls):
return cls.objects.all().get()
class Meta:
abstract = True
class AppParameters(SingletonModel, models.Model):
DEFAULT_BALANCE_ALERT_THRESHOLD = models.PositiveIntegerField(default=5)
# other parameters...
It worked pretty well, until I tried to use one of these parameters in a default attribute of a model field:
class Convive(models.Model):
balance_alert_threshold = models.IntegerField(
default=AppParameters.load().DEFAULT_BALANCE_ALERT_THRESHOLD,
blank=True,
null=True)
This seemed to work too, but when I use a script to reinitialise local data, the first manage.py migrate produce a DoesNotExist since my Singleton does not exist yet.
It happens because of a file importing Convive model.
How would you solve this?
Is there a way to "delay" the evaluation of the default field?
Thanks.
EDIT
After posting this, I think that if my code processes db queries at import time, something may be wrong with it...
Create a method that returns the default value,
def get_default_balance_alert_threshold():
return AppParameters.load().DEFAULT_BALANCE_ALERT_THRESHOLD
then use that method as your default.
class Convive(models.Model):
balance_alert_threshold = models.IntegerField(
default=get_default_balance_alert_threshold,
blank=True,
null=True,
)

How to override Model defaults method of 3rd party installed app, django?

I just installed this django-csvimport package. Now I want to override the default values in the Admin area form. I found the code here, which defines the models, and contains the current default text:
class CSVImport(models.Model):
""" Logging model for importing files """
model_choice = []
model_name = models.CharField(max_length=255, blank=False,
default='csvimport.Item',
help_text='Please specify the app_label.model_name',
choices=get_models())
field_list = models.TextField(blank=True,
help_text='''Enter list of fields in order only if
you dont have a header row with matching field names, eg.
"column1=shared_code,column2=org(Organisation|name)"''')
upload_file = models.FileField(upload_to='csv', storage=fs)
file_name = models.CharField(max_length=255, blank=True)
encoding = models.CharField(max_length=32, blank=True)
upload_method = models.CharField(blank=False, max_length=50,
default='manual', choices=CHOICES)
error_log = models.TextField(help_text='Each line is an import error')
import_date = models.DateField(auto_now=True)
import_user = models.CharField(max_length=255, default='anonymous',
help_text='User id as text', blank=True)
def error_log_html(self):
return re.sub('\n', '<br/>', self.error_log)
error_log_html.allow_tags = True
def __unicode__(self):
return self.upload_file.name
So for example I would like override the model_name field default csvimport.Item with something else. I am a bit at a loss how to override this as I do not have an app folder for csvimport, as its a 3rd part installation. It will be my first time overriding a 3rd party installed app model.
Now that I look into it a bit more, not sure if I should override this model or perhaps better the ModelAdmin class of the admin.py file?
Thanks!
"""Your admin.py"""
from csvimport.models import CSVImport
from csvimport.admin import CSVImportAdmin
class MyCSVImportAdmin(CSVImportAdmin):
"""Override some of the form's field properties:
clean, creation_counter, default_error_messages,
default_validators, disabled, empty_value, empty_values .. etc
"""
def get_form(self, request, obj=None, **kwargs):
form = super(MyCSVImportAdmin, self).get_form(request, obj, **kwargs)
form.base_fields["model_name"].initial = 'What you want'
form.base_fields["model_name"].help_text = 'Please customize the fields however you like'
return form
admin.site.unregister(CSVImport)
admin.site.register(CSVImport, MyCSVImportAdmin)
I saw the whole code and django-csvimport package does not provide you the functionality to override anything from their code so its not possible to override without copying app to your project. Below is an example of another app django-oauth-toolkit which uses a user settings param to provide the functionality of modifications.
USER_SETTINGS = getattr(settings, "OAUTH2_PROVIDER", None)
Now the solution would be only to copy the app and then modify the app for your own usage.

Categories

Resources