Я пытаюсь заполнить записать модели из оболочки, и я пытался заполнить информацию для ввода (ForeignKey), которая делает его доступным для другого класса.это мой код в pycharm:
class Team(models.Model):
name = models.CharField(max_length=256, unique=True)
details = models.TextField()
def __str__(self):
return self.name
class Player(models.Model):
name = models.CharField(max_length=256)
number = models.IntegerField()
age = models.IntegerField()
position_in_field = models.CharField(max_length=256, choices=(('1', 'حارس'), ('2', 'دفاع'), ('3', 'وسط'), ('4', 'هجوم')))
is_captain = models.BooleanField(default=False)
team = models.ForeignKey(Team)
def __str__(self):
return '{} - {}'.format(self.name, self.team)
и вот результат:
python manage.py shell
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 22:20:52) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from teams.models import Player
>>> from teams.models import Team
>>> Player.objects.create(name='محمد إبراهيم', number='25', age='27', position_in_field='هجوم', is_captain=False, team='فريق الزمالك')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "D:\cj\projects\django\teammanager_env\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "D:\cj\projects\django\teammanager_env\lib\site-packages\django\db\models\query.py", line 392, in create
obj = self.model(**kwargs)
File "D:\cj\projects\django\teammanager_env\lib\site-packages\django\db\models\base.py", line 555, in __init__
_setattr(self, field.name, rel_obj)
File "D:\cj\projects\django\teammanager_env\lib\site-packages\django\db\models\fields\related_descriptors.py", line 216, in __set__
self.field.remote_field.model._meta.object_name,
ValueError: Cannot assign "'فريق الزمالك'": "Player.team" must be a "Team" instance.