Django程序中,在 model TextCopy中多次把 LoginDevice作为外键就会报错怎么办?
程序中,在 model TextCopy中多次把 LoginDevice作为外键就会报错怎么办?
完整代码:
from django.db import modelsfrom django.contrib import admin
from datetime import datetime
import random
import string
# Create your models here.
class User(models.Model):
user_id = models.IntegerField('user id', primary_key=True, help_text='user unique id')
nickname = models.CharField(max_length=20, default='User_' + ''.join(random.sample(string.ascii_letters + string.digits,5)))
passwd_hash = models.CharField('password hash', max_length=200, blank=True)
email_addr = models.CharField('email address', max_length=320, blank=True)
phone_num = models.CharField('phone number', max_length=30, blank=True)
def __str__(self):
return self.nickname
class LoginDevice(models.Model):
device_name = models.CharField('device name', max_length=100)
device_id = models.CharField('device id', max_length=100, unique=True)
is_mobile_device = models.BooleanField('is mobile device') # 是否为移动设备
bind_time = models.DateTimeField('device bind time', default=datetime.now()) # 设备首次登录日期
user = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.device_name
class TextCopy(models.Model):
content = models.TextField('copy content')
time = models.DateTimeField(default=datetime.now())
user = models.ForeignKey(User, on_delete=models.CASCADE)
from_device = models.ForeignKey(LoginDevice, null=True, on_delete=models.SET_NULL, related_name='from_device')
to_devices = models.ManyToManyField(LoginDevice, blank=True, null=True, through='TextCopyDeviceMemberShip')
informed_devices = models.ManyToManyField(LoginDevice, blank=True, null=True, through='TextCopyDeviceMemberShip')
def __str__(self):
return self.content
class TextCopyDeviceMemberShip(models.Model):
device = models.ForeignKey(LoginDevice, on_delete=models.CASCADE)
text_copy = models.ForeignKey(TextCopy, on_delete=models.CASCADE)
错误信息:
(venv) george@george-MS-7B89:~/Projects/copy_cloud_server$ python manage.py makemigrationsSystemCheckError: System check identified some issues:
ERRORS:
app.TextCopy.informed_devices: (fields.E304) Reverse accessor for 'app.TextCopy.informed_devices' clashes with reverse accessor for 'app.TextCopy.to_devices'.
HINT: Add or change a related_name argument to the definition for 'app.TextCopy.informed_devices' or 'app.TextCopy.to_devices'.
app.TextCopy.to_devices: (fields.E304) Reverse accessor for 'app.TextCopy.to_devices' clashes with reverse accessor for 'app.TextCopy.informed_devices'.
HINT: Add or change a related_name argument to the definition for 'app.TextCopy.to_devices' or 'app.TextCopy.informed_devices'.
app.TextCopy: (models.E003) The model has two identical many-to-many relations through the intermediate model 'app.TextCopyDeviceMemberShip'.
WARNINGS:
app.LoginDevice.bind_time: (fields.W161) Fixed default value provided.
HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
app.TextCopy.informed_devices: (fields.W340) null has no effect on ManyToManyField.
app.TextCopy.time: (fields.W161) Fixed default value provided.
HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
app.TextCopy.to_devices: (fields.W340) null has no effect on ManyToManyField.
以上是 Django程序中,在 model TextCopy中多次把 LoginDevice作为外键就会报错怎么办? 的全部内容, 来源链接: utcz.com/p/938053.html