Jestem bardzo nowy w Django. Postępowałem z tutorialami, aby nauczyć się ramy.

Używam Django w Virtualenv. Kiedy uruchomię polecenie

python manage.py makemigrations

Otrzymuję poniższy błąd

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/phyadavi/django-project/django-env/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/Users/phyadavi/django-project/django-env/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
    django.setup()
  File "/Users/phyadavi/django-project/django-env/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/phyadavi/django-project/django-env/lib/python3.7/site-packages/django/apps/registry.py", line 112, in populate
    app_config.import_models()
  File "/Users/phyadavi/django-project/django-env/lib/python3.7/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/phyadavi/django-project/django-env/tictactoe/gameplay/models.py", line 5, in <module>
    class Game(models.Model):
  File "/Users/phyadavi/django-project/django-env/tictactoe/gameplay/models.py", line 7, in Game
    related_name="games_first_player")
TypeError: __init__() missing 1 required positional argument: 'on_delete'

Oto klasa modelu

from django.db import models
from django.contrib.auth.models import  User


class Game(models.Model):
    first_player = models.ForeignKey(User,
                                     related_name="games_first_player")
    second_player = models.ForeignKey(User,
                                      related_name="games_second_player")

    start_time = models.DateTimeField(auto_now_add=True)
    last_active = models.DateTimeField(auto_now=True)

class Move(models.Model):
    x = models.IntegerField()
    y = models.IntegerField()
    comment = models.CharField(max_length=300, blank=True)
    by_first_player = models.BooleanField()

    game = models.ForeignKey(Game, on_delete=models.CASCADE)

Doceń swoją pomoc w zrozumieniu tej kwestii i naprawienia tego.

0
wandermonk 1 marzec 2019, 13:37

2 odpowiedzi

Najlepsza odpowiedź

Ponieważ Django 2.0 Field Roundey wymaga dwóch argumentów pozycyjnych:

  1. model do mapowania
  2. argument On_delete.

Przeczytaj Dokumentacja i odpowiednio dodaj on_delete do poniższego.

first_player = models.ForeignKey(User,
                                related_name="games_first_player")
second_player = models.ForeignKey(User,
                                related_name="games_second_player")
2
Shubham Rath 1 marzec 2019, 10:45

Począwszy od Django 2.0, Parametr on_delete jest obowiązkowy dla wszystkich pola ForeignKey. Musisz zmienić swój pierwszy i drugi gracz:

class Game(models.Model):
    first_player = models.ForeignKey(
        User, related_name="games_first_player", on_delete=models.CASCADE
    )
    second_player = models.ForeignKey(
        User, related_name="games_second_player", on_delete=models.CASCADE
    )

    start_time = models.DateTimeField(auto_now_add=True)
    last_active = models.DateTimeField(auto_now=True)
2
mfrackowiak 1 marzec 2019, 10:41