Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading fixture with many-to-one relations in Django

The Django tutorial on djangoproject.com gives a model like this:

import datetime
from django.utils import timezone
from django.db import models

class Poll(models.Model):
    question = models.CharField(max_length = 200)
    pub_date = models.DateTimeField('date published')

    def __unicode__(self):
        return self.question

    def was_published_recently(self):
        now = timezone.now()
        return now - datetime.timedelta(days = 1) <= self.pub_date < now

    was_published_recently.admin_order_field = 'pub_date'
    was_published_recently.boolean = True
    was_published_recently.short_description = 'Published recently?'

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice_text = models.CharField(max_length = 200)
    votes = models.IntegerField(default = 0)

    def __unicode__(self):
        return self.choice_text

Choice uses a ForeignKey, which is a many-to-one relation, so I should be able to use a Choice for more than one Poll. If I try to load this from a fixture, like so:

[
    {
        "model": "polls.Poll",
        "pk": 3,
        "fields": {
            "question": "What time do you sleep?",
            "pub_date": "2013-07-29T10:00:00+00:00"
        }
    },
    {
        "model": "polls.Poll",
        "pk": 4,
        "fields": {
            "question": "What time do you get up?",
            "pub_date": "2013-07-29T10:00:00+00:00"
        }
    },
    {
        "model": "polls.Choice",
        "pk": 4,
        "fields": {
            "poll": [{"pk": 3}, {"pk": 4}],
            "choice_text": "10:00",
            "votes": 0
        }
    }
]

I get this error:

    DeserializationError: Problem installing fixture '/.../poll/polls/fixtures/initial_data.json': [u"'[{u'pk': 3}, {u'pk': 4}]' value must be an integer."]

Or so:

{
        "model": "polls.Choice",
        "pk": 4,
        "fields": {
            "poll": [3, 4],
            "choice_text": "10:00",
            "votes": 0
        }
    }

I get this error:

DeserializationError: Problem installing fixture '/.../poll/polls/fixtures/initial_data.json': [u"'[3, 4]' value must be an integer."]

How can I load many-to-one relations from a fixture?

like image 432
henrikstroem Avatar asked Jul 29 '13 08:07

henrikstroem


People also ask

How does Django handle one to many relationships?

One to many relationships in Django models. To define a one to many relationship in Django models you use the ForeignKey data type on the model that has the many records (e.g. on the Item model). Listing 7-22 illustrates a sample of a one to many Django relationship.

How do I load all fixtures in Django?

Show activity on this post. This command will look for the folder fixture in all the directory and then it will pick up all the files with json extension and will install the fixtures. This way you won't have to have just one folder for fixtures instead you can have fixtures on app level and in multiple apps.

How do I create a fixture in Django?

You must create a directory in your app named fixtures and put your fixtures files there. You can write them in json or xml, one easy way to make them is to create some objects in the admin interface and then run manage.py dumpdata. That would dump the data from the objects you created into fixture files.

What is Django ForeignKey model?

ForeignKey is a Django ORM field-to-column mapping for creating and working with relationships between tables in relational databases. ForeignKey is defined within the django. db. models. related module but is typically referenced from django.


1 Answers

Here's a quote from the tutorial:

Finally, note a relationship is defined, using ForeignKey. That tells Django each Choice is related to a single Poll. Django supports all the common database relationships: many-to-ones, many-to-manys and one-to-ones.

Each Choice is related to a single Poll and you are trying to pass a list of keys to the Choice.poll field.

But, each poll can be related to several choices:

{
    "pk": 4, 
    "model": "polls.Choice", 
    "fields": {
        "votes": 0, 
        "poll": 2, 
        "choice_text": "Meh."
    }
}, 
{
    "pk": 5, 
    "model": "polls.Choice", 
    "fields": {
        "votes": 0, 
        "poll": 2, 
        "choice_text": "Not so good."
    }
}

Hope that helps.

like image 189
alecxe Avatar answered Sep 19 '22 07:09

alecxe