我正在尝试迁移这两个模型:
public abstract class BaseModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public abstract ModelType ModelType { get; }
public void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class DisplayModel1 : BaseModel
{
public string Name { get; set; }
public override ModelType ModelType => ModelType.Model1;
}
public class DisplayModel2 : BaseModel
{
public string Name { get; set; }
public string ExtraText { get; set; }
public override ModelType ModelType => ModelType.Model2;
}
public class TabModel
{
private bool isSelected;
public string Name { get; set; }
public bool IsSelected
{
get { return isSelected; }
set { isSelected = value; }
}
public BaseModel ViewModel { get; set; }
}
//MainViewModel
public class ViewModel
{
public ObservableCollection<TabModel> TabItems { get; set; }
public ViewModel()
{
TabItems = new ObservableCollection<TabModel>();
TabItems.Add(new TabModel()
{
Name = "Tab1",
ViewModel = new DisplayModel1() { Name = "ModelOne" },
IsSelected = true
});
TabItems.Add(new TabModel()
{
Name = "Tab1",
ViewModel = new DisplayModel2() { Name = "ModelTwo", ExtraText = "ExtraTwo" },
IsSelected = false
});
}
}
//Decission, which View should be used
public enum ModelType
{
Model1,
Model2
}
但是我遇到了这个错误:
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
# Create your models here.
# Groups Model
class Cluster(models.Model):
grp_name = models.CharField(max_length=100)
grp_status = models.CharField(max_length=100)
grp_description = models.TextField()
created_on = models.DateTimeField(default=timezone.now)
created_by = models.ForeignKey(
User,
related_name='created',
on_delete=models.CASCADE
)
updated_on = models.DateTimeField(auto_now=True)
members = models.ManyToManyField(
User,
through='UserGroup',
through_fields=('user', 'group'),
related_name='members'
)
def __str__(self):
return self.grp_name
#user groups
class UserGroup(models.Model):
user = models.ForeignKey(
User,
on_delete=models.CASCADE,
related_name = 'user'
)
group = models.ForeignKey(
Cluster,
on_delete=models.CASCADE,
related_name = 'group'
)
date_joined = models.DateTimeField(default=timezone.now)
我想在用户和组之间建立多对多关系。也就是说用户可以处于多个组中,并且组中将有许多用户。为什么我的代码错误?我该如何解决?请帮忙。
答案 0 :(得分:0)
User related_name参数:
created_by=models.ForeignKey(User, on_delete=models.CASCADE,related_name="created")