Python/Django TangoWithDjango Models and Databases

后端 未结 3 853
别跟我提以往
别跟我提以往 2021-01-23 07:03

I\'m currently following http://www.tangowithdjango.com and I\'m trying to populate an existing DB with populate_rango.py. When I create the new categories, I\'m tr

相关标签:
3条回答
  • 2021-01-23 07:24

    The following worked for me, when added to populate_rango.py -

    def add_cat(name, views, likes):
        c = Category.objects.get_or_create(name=name)[0]
        c.views = views
        c.likes = likes
        c.save()
        return c
    

    The difference might be due to c.save()

    0 讨论(0)
  • 2021-01-23 07:26

    The following worked for me. I have no idea whether it will cause issues in later chapters, but it allowed the populate_rango.py script to run.

    Changes to populate_rango.py

    def populate():
        python_cat = add_cat("Python", views=128, likes=64)
    
        django_cat = add_cat("Django", views=64, likes=32) 
    
        frame_cat = add_cat("Other Frameworks", views=32, likes=16)
    

    and

    def add_cat(name, views=0, likes=0):
        c = Category.objects.get_or_create(name=name, views=views, likes=likes)[0]
        return c
    

    That resulted in a database IntegrityError so I edited the View fields in my Category and Page classes to ensure they aren't unique:

    Changes to models.py

    class Category(models.Model):
        views = models.IntegerField(default=0, unique=False)
    
    class Page(models.Model):
        views = models.IntegerField(default=0, unique=False)
    

    Finally, I deleted the database and ran

    python manage.py syncdb
    

    and

    python populate_rango.py
    
    0 讨论(0)
  • 2021-01-23 07:41

    I didn't know we needed to complete the exercises of CH5 when doing CH6, luckily I came across your question and this helped me get the desired output I wanted from the index.html.

    I haven't done any work with the admin side of things though, but it looks like your error is here:

    populate_rango.py

        frame_cat = add_cat("Other Frameworks", 32, 16)
    

    and

    def add_cat(name, views, likes):
    c = Category.objects.get_or_create(name=name, views=views, likes=likes)[0]
    return c
    

    You need to make the views=0 & likes=0 in this line: def add_cat(name, views, likes):

    Then you need to change this:

    frame_cat = add_cat("Other Frameworks", 32, 16) to this:

    frame_cat = add_cat("Other Frameworks", views=32, likes=16)

    Also, you will have a database called DATABASE_PATH (or at least that is what mine is called). You need to delete this and run:

    python manage.py syncdb

    & then just to make sure it worked, I ran:

    python populate_rango.py

    When you got the error in the admin section, did you first check the /rango page to see if you were getting the correct output? Chances are, you were also either getting an error like me or your categories weren't displaying (I could be wrong though on your output).

    I think my solution will display the views and likes now. Try it and let me know.

    0 讨论(0)
提交回复
热议问题