所以我有这样的模型:
class Leaderboard(models.Model):
pass
class Column(models.Model):
leaderboard = models.ForeignKey(Leaderboard, related_name="columns", on_delete=models.CASCADE)
related_columns = models.ManyToManyField(self)
index = models.PositiveIntegerField()
序列化器如此:
class ColumnSerializer(ModelSerializer):
related_columns = serializers.PrimaryKeyRelatedField(queryset=Column.objects.all(), many=True)
class Meta:
model = Column
fields = ('leaderboard', 'related_columns', 'index',)
class LeaderboardSerializer(ModelSerializer):
children = ColumnSerializer(many=True)
class Meta:
model = Leaderboard
fields = ('columns',)
因此,我要做的是验证添加到related_columns
的{{1}}的任何列是否已属于其ColumnSerializer
父级。我已经多次尝试访问排行榜或排行榜ID(例如通过在字段中手动指定Leaderboard
)来创建id
以进行验证,但LeaderboardSerializer`未在Column之前初始化,因此我无法验证细节。
基本上,我想将ColumnSerializer
修改为queryset=Column.objects.all()
但我无法访问queryset=self.instance.leaderboard.columns.all()
内的Leaderboard
。例如,如果我在Column
内访问self.parent.instance/initial
,则ColumnSerializer
为None
,直到Leaderboard.validate_columns()
。我想到的一件事就是在排行榜方面进行验证,但我仍然认为在Column
内进行此验证应该是“可行的”,以防我想直接编辑这些验证,而不首先通过Leaderboard
......
答案 0 :(得分:0)
这是我解决此问题的方法:
library(dplyr)
library(ggplot2)
library(ggsignif)
reach <- c("Pool 4", "Pool 8", "Pool 16", "Pool 26", "LaGrange", "Open River")
n <- c(30, 6, 3, 29, 12, 76)
avg <- c(812, 819, 674, 649, 698, 679)
se <- c(24, 32, 43, 21, 31, 12)
dat <- data.frame(reach, n, avg, se)
dat <- dat %>%
mutate(reach = factor(reach, levels = reach, ordered = TRUE))
pos_nudge = 10
ggplot(dat,
aes(x = reach,
y = avg)) +
geom_point() +
geom_linerange(aes(x = reach,
ymin = avg - se,
ymax = avg + se)) +
geom_signif(y_position = max(avg + se + pos_nudge),
xmin = c(1, 3),
xmax = c(2, 4),
annotation = c("A", "B"),
tip_length = 0) +
geom_signif(y_position = max(avg + se + pos_nudge),
xmin = c(5, 6),
xmax = c(5, 6),
annotation = c("B", "B"),
tip_length = 0,
size = 0)
我确保这些列是唯一的(在它们的键和索引中),并且它们所引用的列也存在。