Trying to make a blog on Django. For this I use SQLite. I get a one-to-many connection - articles to the comments:

#! /usr/bin/env python # -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import models # Create your models here. class Article(models.Model): class Meta(): db_table = "article" article_title = models.CharField(max_length=200) article_text = models.TextField() article_date = models.DateTimeField() article_likes = models.IntegerField(default=0) class Comments(models.Model): class Meta(): db_table = 'comments' comments_text = models.TextField() comments_article = models.ForeignKey(Article) 

There is such an error:

 You are trying to add a non-nullable field 'comments_article' to comments without a default; we can't do that (the database needs something to populate existing rows). 

Where do I need to set a default value? What am I doing wrong ?

    1 answer 1

    Most likely this is due to the fact that previously added comments are already stored in the database and after migration they will not be tied to any article that does not fit the model. It is necessary to delete all comments from the database, for example, using the administrative panel Django.

    • And you can about how this can be done? - faoxis
    • If the admin panel is enabled in the settings and a super-user is created for the project, you need to register the comment model by adding 2 lines to the admin.py file (which is in the application folder): from myproject.myapp.models import Comments and admin.site.register(Comments) and then you can go to the admin. The panel under the standard link /admin/ and there you can already delete / add / edit entries. Read more: djbook.ru/rel1.9/ref/contrib/admin/index.html and here: djbook.ru/rel1.9/ref/… - user5720164
    • But in general, is it right to do that with comments? Maybe it would be better to create a new application? - faoxis
    • This only affects visibility. You can split at any time, for example, when this application becomes too big. As for the blog, I advise the first 2 playlists from here: codingforentrepreneurs.com/projects There's just about building a blog with comments, plus the source is. But you need to understand English. - user5720164
    • but the site is paid - faoxis