我有两个类,其中一个是另一个类的后代,我想让它们成为同一基类的同类兄弟类.
之前:
from django.db import models
class A(models.Model):
name = models.CharField(max_length=10)
class B(models.Model):
title = models.CharField(max_length=10)
Run Code Online (Sandbox Code Playgroud)
后:
from django.db import models
class Base(models.Model):
name = models.CharField(max_length=10)
class A(Base):
pass
class B(Base):
title = models.CharField(max_length=10)
Run Code Online (Sandbox Code Playgroud)
当我生成模式迁移时,这是输出,包括我对问题的回答:
+ Added model basetest.Base
? The field 'B.a_ptr' does not have a default specified, yet is NOT NULL.
? Since you are removing this field, you MUST specify a default
? value to use for existing rows. Would you like to:
? 1. Quit now, …Run Code Online (Sandbox Code Playgroud) 我有一个需要反复执行的任务,所以我试图为它编写一个函数.我的函数几乎完成,除了一个缺陷:我不能将公式作为参数传递,除非它是一个字符串的形式.
library(lme4)
library(lazyeval)
get_pvals_qrhs <- function(df, cols, qrhs) {
cols <- substitute(cols)
col_pos <- setNames(as.list(seq_along(df)), names(df))
pos <- eval(cols, col_pos)
formulas <- lapply(pos, function(x) formula(paste(colnames(df[x]), "~", qrhs)))
models <- lapply(formulas, lmer, data=df, REML=FALSE)
tvals <- lapply(models, function(x) data.frame(coef(summary(x)))[c(2), ]$t.value)
pvals <- lapply(tvals, function(x) { 2 * (1 - pnorm(abs(x))) })
return(unlist(pvals))
}
works <- get_pvals_qrhs(iris, Sepal.Length:Sepal.Width, "Species + (1 + Petal.Length | Petal.Width)")
works
get_pvals_rhs <- function(df, cols, rhs) {
cols <- substitute(cols)
col_pos <- setNames(as.list(seq_along(df)), names(df))
pos <- eval(cols, …Run Code Online (Sandbox Code Playgroud) 给定这样的数据框:
station <- c(1, 2, 3, 1, 2, 3, 1, 2, 2, 2)
obs <- c(12.3, 10.4, 9.8, 15.9, 8.2, 8.4, 6.3, 10.2, 9.0, 8.3)
df <- data.frame(station, obs)
Run Code Online (Sandbox Code Playgroud)
我想像这样创建一个新列run:
station obs run
1 1 12.3 1
2 2 10.4 1
3 3 9.8 1
4 1 15.9 2
5 2 8.2 2
6 3 8.4 2
7 1 6.3 3
8 2 10.2 3
9 2 9.0 4
10 2 8.3 5
Run Code Online (Sandbox Code Playgroud)
如果我用不同的语言写这个,我的伪代码看起来像这样:
run := 1
if …Run Code Online (Sandbox Code Playgroud)