Python/Django TangoWithDjango Models and Databases

后端 未结 3 901
别跟我提以往
别跟我提以往 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: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
    

提交回复
热议问题