0

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)
aschultz
  • 1,658
  • 3
  • 20
  • 30
Serenity
  • 3,884
  • 6
  • 44
  • 87

2 Answers2

4

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.

Amit
  • 2,018
  • 1
  • 8
  • 12
  • how should i use apps.get_model in my filter function then? Can you help me at this, please? – Serenity Jul 29 '19 at 12:37
  • I want to return all the package subclasses that is why I was using `is_content_model = lambda m: m is not Package and issubclass(m, Package)` and filter to filter out the model using `return list(filter(is_content_model, models.get_models()))` – Serenity Jul 29 '19 at 15:20
  • this one solved for me `return list(filter(is_content_model, apps.get_models()))`. Thanks a lot for your help – Serenity Jul 29 '19 at 15:28
0
from django.apps import apps

ModelClass = apps.get_model('app_name.ModelClass')

You can now instatiate this class

mc = ModelClass()

Doc here