Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

465
Visualizações
Why does django's `apps.get_model()` return a `__fake__.MyModel` object

I am writing a custom Django migration script. As per the django docs on custom migrations, I should be able to use my model vis-a-vis apps.get_model(). However, when trying to do this I get the following error:

AttributeError: type object 'MyModel' has no attribute 'objects'

I think this has to do with the apps registry not being ready, but I am not sure.

Sample code:

def do_thing(apps, schema_editor):
    my_model = apps.get_model('app', 'MyModel')

    objects_ = my_model.objects.filter(
        some_field__isnull=True).prefetch_related(
        'some_field__some_other_field')  # exc raised here


class Migration(migrations.Migration):

    atomic = False

    dependencies = [
        ('app', '00xx_auto_xxx')
    ]

    operations = [
        migrations.RunPython(do_thing),
    ]

A simple print statement of apps.get_model()'s return value shows the following: <class '__fake__.MyModel'>. I'm not sure what this is, and if it is a result of not being ready.

EDIT:

I couldn't find any resources to explain why I am getting a __fake__ object so I decided to tinker with the code. I got it to work by preempting apps from args, as can be seen here:

def do_thing(apps, schema_editor):
    from django.apps import apps

    my_model = apps.get_model('app', 'MyModel')

    objects_ = my_model.objects.filter(
        some_field__isnull=True).prefetch_related(
        'some_field__some_other_field')  # no more exc raised here

I am still confused and any help would be appreciated.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The fake objects are historical models. Here's the explanation from Django docs:

When you run migrations, Django is working from historical versions of your models stored in the migration files.

[...]

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).

In case objects is a custom manager, you can set use_in_migrations = True to make it available in migrations.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda