rh0*_*ium 5 django mysqldump django-models django-admin
有没有人有一个简单的解决方案来使用(或修改)dumpdata将一个简单的表缩小到最后n行。我喜欢将转储数据用于测试夹具,但是数据大小太大了,这毫无意义。顺便说一句-我不是在设计桌子,我只是需要处理的吸盘。
对于那些可能会问这里结构如何的人。
从Django Side
class GridResourceUsage(models.Model):
"""Sampled point in time of license usage for individual grid resource. Includes who and quanity."""
timestamp = models.DateTimeField(db_index=True)
grid_license_resource = models.ForeignKey(GridLicResource)
total = models.IntegerField(default=None, null=True)
limit = models.IntegerField(default=None, null=True)
free = models.IntegerField(default=None, null=True)
intern = models.IntegerField(default=None, null=True)
extern = models.IntegerField(default=None, null=True)
waiting = models.IntegerField(default=None, null=True)
def __unicode__(self):
return str("GRU-" + self.grid_license_resource.name)
class Meta:
ordering = ['-timestamp']
@models.permalink
def get_absolute_url(self):
return('hist_res_id', (), {'resource': str(self.grid_license_resource.name), 'id':str(self.id)})
Run Code Online (Sandbox Code Playgroud)
从MySQL方面
CREATE TABLE `gridresource_gridresourceusage` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`timestamp` datetime NOT NULL,
`grid_license_resource_id` int(11) NOT NULL,
`total` int(11) DEFAULT NULL,
`limit` int(11) DEFAULT NULL,
`free` int(11) DEFAULT NULL,
`intern` int(11) DEFAULT NULL,
`extern` int(11) DEFAULT NULL,
`waiting` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `gridresource_gridresourceusage_timestamp` (`timestamp`),
KEY `gridresource_gridresourceusage_grid_license_resource_id` (`grid_license_resource_id`)
) ENGINE=MyISAM AUTO_INCREMENT=2891167 DEFAULT CHARSET=latin1;
Run Code Online (Sandbox Code Playgroud)
我不确定是否dumpdata可以满足您的要求。
您不能只创建查询集并将其序列化吗?有点愚蠢的例子,但它应该可以工作。
# Create queryset you want
n = SomeModel.objects.count()-1 # Row offset counting from the end
queryset = SomeModel.objects.all()[n:]
# Serialize that queryset to json in this case
from django.core import serializers
data = serializers.serialize("json", queryset)
# And write it into the file
f = open('data.json', 'w')
f.write(data)
f.close()
Run Code Online (Sandbox Code Playgroud)
您可以将其包装在管理命令中,并以与使用命令相同的方式或多或少地使用它dumpdata。(您也可以查看转储数据的来源以获取建议)
| 归档时间: |
|
| 查看次数: |
695 次 |
| 最近记录: |