[Python] Django manage.py loaddata : 'ManyToOneRel' object has no attribute 'to_python'

Discussão em 'Python' iniciado por Stack, Dezembro 20, 2024.

  1. Stack

    Stack Membro Participativo

    I'm trying to import data from json file using manage.py loaddata. getting below error while importing.

    File "/opt/virtualenvs/python3/lib/python3.8/site-packages/django/core/serializers/python.py", line 144, in Deserializer
    raise base.DeserializationError.WithData(e, d['model'], d.get('pk'), field_value)
    django.core.serializers.base.DeserializationError: Problem installing fixture '/home/runner/BookShop/bookShopAPI/inventory/fixtures/seed_data.json': 'ManyToOneRel' object has no attribute 'to_python': (inventory.author:pk=2) field_value was '[1]'


    models.py

    class Genre(models.Model):
    # TAG Choices
    FICTION = 'F'
    GRAPHIC_NOVEL = 'GN'
    # add more tag options if required
    TAG = (
    (FICTION, 'Fiction'),
    (GRAPHIC_NOVEL, 'Graphic Novel'),
    )
    # write implementation
    tag = models.CharField(max_length=15,choices=TAG)
    description = models.TextField(max_length=100)
    author = models.ForeignKey(Author,on_delete=models.CASCADE,related_name='genre',null=True, blank=True)
    book = models.ForeignKey('Book',on_delete=models.CASCADE,related_name='genre',null=True, blank=True)


    class Author(models.Model):
    # write implementation
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    author_bio = models.TextField(max_length=100)
    book = models.ForeignKey('Book',on_delete=models.CASCADE,related_name='author',null=True, blank=True)


    json file

    [
    {
    "model":"inventory.genre",
    "pk":"1",
    "fields":{
    "tag":"fiction",
    "description":"Fiction is the telling of stories which are not real. More specifically, fiction is an imaginative form of narrative, one of the four basic rhetorical modes. Although the word fiction is derived from the Latin fingo, fingere, finxi, fictum, works of fiction need not be entirely imaginary and may include real people, places, and events. Fiction may be either written or oral. Although not all fiction is necessarily artistic, fiction is largely perceived as a form of art or entertainment. The ability to create fiction and other artistic works is considered to be a fundamental aspect of human culture, one of the defining characteristics of humanity."
    }
    },
    {
    "model":"inventory.genre",
    "pk":"2",
    "fields":{
    "tag":"graphic novel",
    "description":"A graphic novel is a narrative work in which the story is conveyed to the reader using sequential art in either an experimental design or in a traditional comics format. The term is employed in a broad manner, encompassing non-fiction works and thematically linked short stories as well as fictional stories across a number of genres."
    }
    },
    {
    "model": "inventory.author",
    "pk": "1",
    "fields": {
    "first_name": "Arthur",
    "last_name": "Golden",
    "author_bio": "Arthur Golden was born in Chattanooga, Tennessee, and was educated at Harvard College, where he received a degree in art history, specializing in Japanese art. In 1980 he earned an M.A. in Japanese history from Columbia University, where he also learned Mandarin Chinese. Following a summer in Beijing University, he worked in Tokyo, and, after returning to the United States, earned an M.A. in English from Boston University. He resides in Brookline, Massachusetts, with his wife and two children.",
    "genre":[
    1
    ]
    }
    }]


    Is something wrong with the models ? it seems like its having problem while importing author 1 from json data at filed genre. what should be the correct way to refer genre 1 at author ?

    Continue reading...

Compartilhe esta Página