我有一个模特'读者'它扩展了默认用户'模型:
def loopNplot(NSdf, variantsDF, window):
#store window
windows = list(range(1,101))
#store diversity
diversity = []
#Loop through windows appending to the list.
for i in range(window, 101000, window):
#Create a list to hold counts for each chromosome (which we then take the mean of)
tempList = []
#Loop through chromosomes
for j in NSdf['CHROM']:
#Subset NS polymorphisms
subDF = vtable.loc[(vtable['CHROM'] == j)].copy()
#Subset all variants
subVar = all_variants.loc[(all_variants['CHROM'] == j)].copy()
x = windowAvg(subDF, subVar, i)
#convert series to list
x = x.tolist()
#extend templist to include x
tempList.extend(x)
#Append mean of tempList - counts of diversity - to list.
diversity.append(sum(tempList)/len(tempList))
#Copy diversity
divCopy = list(diversity)
#Add a new first index of 0
diversity = [0] + diversity
#Remove last index
diversity = diversity[:-1]
#x - diversity to give number of variants in each window
div = list(map(operator.sub, divCopy, diversity))
plt.scatter(windows, div)
plt.show()
问题1 :我可以直接创建一个Reader而不是User(无需将读者与我现在在管理界面中看到的现有用户相关联),在这种情况下应该如何我的POST电话看起来像?对于目前注册新用户,我需要用户名,密码1,密码2。
问题2 :在创建阅读器时,我还想为该阅读器创建心愿单。如何实现?
到目前为止,我有一个序列化器:
class Wishlist(models.Model):
wishlist_id = models.AutoField(primary_key=True)
class Reader(models.Model):
user = models.OneToOneField(User)
phone = models.CharField(max_length=30)
address = models.CharField(max_length=80)
dob = models.DateField(auto_now=False, auto_now_add=False, null=True, blank=True)
# A library has many readers
which_library = models.ForeignKey('Library', related_name='readers', on_delete=models.CASCADE)
wishlist = models.OneToOneField(Wishlist, null=True, blank=True)
#This helps to print in admin interface
def __str__(self):
return u"%s" % (self.user)
观点:
class ReaderSerializer(serializers.ModelSerializer):
class Meta:
model = Reader
fields = '__all__'
#depth = 1
def create(self, validated_data):
wishlist_data = validated_data.pop('wishlist')
reader = Reader.objects.create(**validated_data)
Wishlist.objects.create(reader=reader, **wishlist_data)
return reader
我认为我的序列化程序会自动创建一个wishlist实例,但它并没有。