I'm trying to make a simple app for users to create simple tests/questionnaires for each other, where they can specify which data type should apply to the answer for each question. To this end, I'm trying to get django-eav2 working, following this guide. However, I run into errors as soon as I try to makemigrations.
Using eav.register:
from django.db import models
from django.utils.translation import ugettext as _
from eav.models import Attribute
class Question(models.Model):
class DataType(models.TextChoices):
TEXT = 'TXT', _('Text')
INTEGER = 'INT', _('Integer (whole number)')
FLOAT = 'FLT', _('Float (number with fractional parts, e.g. 3.14159)')
question_text = models.CharField(max_length=400)
question_type = models.CharField(max_length=3, choices=DataType.choices, default=DataType.TEXT)
class Answer(models.Model):
question = models.ManyToManyField(Question)
import eav
eav.register(Answer)
Attribute.objects.create(slug='txt_value', datatype=Attribute.TYPE_TEXT)
Attribute.objects.create(slug='int_value', datatype=Attribute.TYPE_INT)
Attribute.objects.create(slug='flt_value', datatype=Attribute.TYPE_FLOAT)
I get "django.db.utils.OperationalError: no such table: eav_attribute"
Using decorators:
from django.db import models
from django.utils.translation import ugettext as _
from eav.models import Attribute
from eav.decorators import register_eav
#Dynamic user-specified data types is tricky, see
# https://stackoverflow.com/questions/7933596/django-dynamic-model-fields
class Question(models.Model):
class DataType(models.TextChoices):
TEXT = 'TXT', _('Text')
INTEGER = 'INT', _('Integer (whole number)')
FLOAT = 'FLT', _('Float (number with fractional parts, e.g. 3.14159)')
question_text = models.CharField(max_length=400)
question_type = models.CharField(max_length=3, choices=DataType.choices, default=DataType.TEXT)
@register_eav
class Answer(models.Model):
question = models.ManyToManyField(Question)
Attribute.objects.create(slug='txt_value', datatype=Attribute.TYPE_TEXT)
Attribute.objects.create(slug='int_value', datatype=Attribute.TYPE_INT)
Attribute.objects.create(slug='flt_value', datatype=Attribute.TYPE_FLOAT)
I get "TypeError: register_eav() takes 0 positional arguments but 1 was given"
The venv python version is Python 3.9.7, django 3.1.13, django-eav2==1.1.0. I also have django-cms==3.8.0 in the project although it's not in this app directory, if that makes a difference. I'm completely new to Django so I may well have made a mistake at that level.
Any idea where I'm going wrong here? I can't even get started and I'm feeling really stupid.