Cannot generate instances of abstract factory (Django factory_boy)

48 Views Asked by At

These are factories:

import factory
from .models import *
from factory.faker import *

FAKE = faker.Faker(locale = 'ru_RU')

class RoleFactoryManager(factory.django.DjangoModelFactory):
    
    class Meta:
        model = Role
        abstract = False
    role_name = 'manager'

class RoleFactoryAdmin(factory.django.DjangoModelFactory):
    class Meta:
        model = Role
        abstract = False
    role_name = 'admin'

class RoleFactoryUser(factory.django.DjangoModelFactory):
    
    class Meta:
        model = Role
        abstract = False
    role_name = 'user'

class ClientFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Client
        abstract = False

    client_surname = factory.Faker('second_name')
    client_name = factory.Faker('first_name')
    client_date_of_registration = factory.Faker('date')
    client_email = factory.LazyAttribute(lambda a: '{}.{}{}@gmail.com'.format(a.client_surname, a.client_name, a.client_id).lower())
    client_phone = factory.Faker('phone_number')
    client_photo = ('/images/profile_photo.png')

these are models:

from django.db import models

class Role(models.Model):
    role_id = models.AutoField(primary_key=True)
    role_name = models.CharField(max_length=30, null=False)

class Client(models.Model):
    client_id = models.AutoField(primary_key=True)
    client_surname = models.CharField(max_length=30, null=False)
    client_name = models.CharField(max_length=30, null=False)
    client_date_of_registration = models.DateField(null=False)
    client_email = models.CharField(max_length=99, null=False)
    client_password = models.CharField(max_length=99, null=False)
    client_phone = models.CharField(max_length=11, null=False)
    client_photo = models.CharField(max_length=500, null=False)
    def __str__(self):
        return f'{self.client_surname} {self.client_name}'`

and I get this for all of these factories:

error is factory.errors.FactoryError: Cannot generate instances of abstract factory RoleFactoryManager; Ensure RoleFactoryManager.Meta.model is set and RoleFactoryManager.Meta.abstract is either not set or False.

How to fix it?

I tried to indicate the model specifically, change imports and etc.

1

There are 1 best solutions below

1
Nitesh Kumar  Singh On

It looks like there might be an issue with how you're defining your factories. The error message suggests that the factories are being treated as abstract, but you intend them to create instances of the corresponding models.

Here's an updated version of your factories:

import factory
from factory.faker import faker
from .models import Role, Client

FAKE = faker.Faker(locale='ru_RU')

class RoleFactoryManager(factory.django.DjangoModelFactory):
    class Meta:
        model = Role
    role_name = 'manager'

class RoleFactoryAdmin(factory.django.DjangoModelFactory):
    class Meta:
        model = Role
    role_name = 'admin'

class RoleFactoryUser(factory.django.DjangoModelFactory):
    class Meta:
        model = Role
    role_name = 'user'

class ClientFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Client

    client_surname = factory.Faker('last_name')
    client_name = factory.Faker('first_name')
    client_date_of_registration = factory.Faker('date')
    client_email = factory.LazyAttribute(lambda a: '{}.{}{}@gmail.com'.format(a.client_surname, a.client_name, a.client_id).lower())
    client_password = factory.Faker('password')
    client_phone = factory.Faker('phone_number')
    client_photo = '/images/profile_photo.png'

Changes made: Removed abstract = False from the Meta class, as it's not necessary for concrete factories.

Used the correct import for faker.

Make sure to adjust the import statements according to your project structure. If the issue persists, please double-check the Role and Client models to ensure there are no issues there.