我创建了models.py文件,当我尝试执行“ makemigrations”命令时,出现以下错误:
return bool(self.related_name) and self.related_name[-1] == '+'
TypeError: 'ModelBase' object does not support indexing
我试图找到论坛回复,但主题主要涉及的是views.py文件中出现的错误。在我的情况下,该文件尚未创建。
对于数据库中更透明的代码,任何帮助将不胜感激。
我的模型。py
from django.db import models
#additives
class TestUser(models.Model):
name = models.CharField(max_length=50)
class Trade(models.Model):
all_trade_here = models.TextField()
class ImageGallery(models.Model):
image = models.ImageField()
class SocialMediaLinks(models.Model):
link = models.URLField()
# Option for premium user
class Prem_PhotoGallery(models.Model):
name_photo_gallery = models.CharField(max_length=50)
image_gallery = models.ForeignKey(ImageGallery,
on_delete=models.CASCADE)
class Prem_PDF(models.Model):
name_pdf = models.CharField(max_length=50)
file = models.FileField()
class Prem_YouTube(models.Model):
name_youtube = models.CharField(max_length=50)
link = models.URLField()
class Prem_VilmVeo(models.Model):
name_vilmveo = models.CharField(max_length=50)
link = models.URLField()
class Prem_Audio(models.Model):
name_audio = models.CharField(max_length=50)
link = models.URLField()
class Prem_Website(models.Model):
name_website = models.CharField(max_length=50)
link = models.URLField()
class Prem_SocialMedia(models.Model):
name_social_button_1 = models.CharField(max_length=50)
links = models.ForeignKey(SocialMediaLinks, on_delete=models.CASCADE)
class Prem_GoogleMaps(models.Model):
name_google_maps = models.CharField(max_length=50)
link = models.URLField()
# element write by user
class Person(models.Model):
#basic information, all write user
name = models.CharField(max_length=100)
surname = models.CharField(max_length=100)
phone = models.CharField(max_length=15)
e_mail = models.EmailField(max_length=100)
website = models.URLField(max_length=250)
photo = models.FileField()
small_photo = models.FileField()
description = models.TextField()
recommended_by = models.ForeignKey(TestUser,
on_delete=models.CASCADE)
my_projects = models.ManyToManyField(Prem_PhotoGallery,
Prem_PDF,
Prem_YouTube,
Prem_VilmVeo,
Prem_Audio,
Prem_Website,
Prem_SocialMedia,
Prem_GoogleMaps,
blank=True)
keywords = models.TextField()
trade = models.ManyToManyField(Trade)
#premium
premium_accounts = models.BooleanField(default=False)
#location
city = models.CharField(max_length=100)
street = models.CharField(max_length=100)
zip_code = models.CharField(max_length=8)
#data joined
time_create_account = models.DateTimeField()
答案 0 :(得分:1)
您对ManyToManyField
的{{1}}配置不正确。 ManyToManyField
采用一个类作为它的第一个位置参数。它还可以接受其他可选参数,例如my_projects
。
您已经传递了多个类作为参数。 related_name
因此会假设这些其他类与可选参数的值相对应,从而导致您看到错误。
您需要将ManyToManyField
分成单独的my_projects
:
ManyToManyField