0

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.

dataduck
  • 588
  • 4
  • 14
  • Those calls to `Attribute.objects.create` are in your model files? Try removing them, I think you're going to have to move them to somewhere that is not run as part of your app startup, a migration or management command – Iain Shelvington Nov 22 '21 at 09:55
  • @IainShelvington thanks for your response. I'm afraid I'm not sure how to follow your advice here. If not in models.py, is there a "right" place where Attribute definitions go? If they are not at least imported here, how do they get associated with the model objects? – dataduck Nov 22 '21 at 10:44
  • So I think the right place is in migration files. Can you try removing the creates from your models though and see if you are able to migrate? – Iain Shelvington Nov 22 '21 at 12:05
  • @IainShelvington I've removed the Attribute.objects.create lines but the error message is unchanged, `TypeError: register_eav() takes 0 positional arguments but 1 was given` – dataduck Nov 22 '21 at 13:20
  • Don't use the decorator, use `eav.register()`. The docs recommend it too – Iain Shelvington Nov 22 '21 at 13:21
  • @IainShelvington OK, eav.register without Attribute.objects create works. Thanks! So where do the attribute definitions go? – dataduck Nov 23 '21 at 09:47
  • @IainShelvington I reread your comment and tried putting the definitions in the migration file - I got another error (`django.core.exceptions.ValidationError: {'name': ['This field cannot be blank.']}`). I noticed you mentioned that the documentation recommends against using decorators, but the only ones I've found have decorators recommended right at the beginning of the getting started section. Is there more comprehensive / beginner friendly documentation I've not found, or is this not a good place for a django newbie to start? I feel like I'm groping around in the dark here. – dataduck Nov 28 '21 at 20:04

0 Answers0