1

I'm trying to populate my db with fixtures with the next command:

python .\manage.py loaddata .\rules\fixtures\rules.json

But I get the next error:

AttributeError: Problem installing fixture 'C:\Users\Ángel - Trabajo\Documents\AVC.\rules\fixtures\rules.json': 'NoneType' object has no attribute 'id'

This is my model:

class Rule(LogsMixin, models.Model):
    """Definición de modelo para Reglas"""

    FIELDS = [
        ('user_name', 'Nombre de usuario'),
        ('user_code', 'Codigo de usuario')
    ]

    string = models.CharField("Cadena de referencia", max_length=100, null=False, default="", blank=False)
    profile = models.ForeignKey(Profile, null=False, default=None, on_delete=models.DO_NOTHING)
    license = models.ForeignKey(License, null=False, default=None, on_delete=models.DO_NOTHING)
    field = models.CharField("Campo objetivo", max_length=50, default='nombre_usuario', choices=FIELDS)
    include = models.BooleanField("Incluye", null=False, default=True)
    order = models.IntegerField("Orden", null=True, default=None)
    uppercase_sensitive = models.BooleanField("Sensible a las mayúsculas", null=False, default=False)
    dateadded = models.DateTimeField("Fecha de inserción", default=datetime.datetime.now)

And this is my json:

[
    {
        "model":"rules.rule",
        "pk":null,
        "fields":{
            "string": "DINAMITZADORA",
            "profile": "1",
            "license": "1",
            "field": "user_name",
            "include": "1",
            "order": "0",
            "uppercase_sensitive": "0"
        }
    },
    {
        "model":"rules.rule",
        "pk":null,
        "fields":{
            "string": "adm",
            "profile": "1",
            "license": "1",
            "field": "user_name",
            "include": "1",
            "order": "0",
            "uppercase_sensitive": "0"
        }
    },
    {
        "model":"rules.rule",
        "pk":null,
        "fields":{
            "string": "admin",
            "profile": "1",
            "license": "1",
            "field": "user_name",
            "include": "1",
            "order": "0",
            "uppercase_sensitive": "0"
        }
    },
    {
        "model":"rules.rule",
        "pk":null,
        "fields":{
            "string": "alum",
            "profile": "1",
            "license": "1",
            "field": "user_name",
            "include": "1",
            "order": "0",
            "uppercase_sensitive": "0"
        }
    },
]

I've tried declare the FIXTURE_DIRS in my settings.py but didnt work. I tried to let only one element (it has like 80) in json just in case it was about some value but it didnt work neither.

I'm completly lost, could you help me?

Thank you guys :)

Gerrit Geeraerts
  • 924
  • 1
  • 7
  • 14
AngelQuesada
  • 387
  • 4
  • 19

1 Answers1

1

Did you type this json yourself?

How i do it, i start with a clean database and than i add some data via the web interface and than i execute the following command:

python .\manage.py dumpdata your_appname --indent 4 > .\rules\fixtures\rules.json

So later on when you have an empty database you can use your command to load the data again:

python .\manage.py loaddata .\rules\fixtures\rules.json

If you specify a path after loaddata, than FIXTURE_DIRS will be ignored so you should not set that in your settings.py file.

Best practice for saving your fixtures is in the location your_app/fixtures/somename.json But i assume this is the case since i think your app is called rules?

If you do this django will know where to look and you can just type instead of providing the whole path.

python .\manage.py loaddata some_name.json
Gerrit Geeraerts
  • 924
  • 1
  • 7
  • 14