我想将所有模型数据导出到CSV文件中:
models.py
import ast
import uuid
import base64
from django.db import models
from django.contrib import admin
from qlu.settings import HOST_NAME,STATS_URI
from django.core.validators import URLValidator
#------------------------------------------------------------------------------
class short_url(models.Model):
"""
This is a short_url class
"""
blocked = models.BooleanField(default=False) # To check whether URL is blocked or not
updated_at = models.DateTimeField(auto_now=True) # When URL is updated
url = models.TextField(validators=[URLValidator()]) # URL entered by the user
created_at = models.DateTimeField(auto_now_add=True) # When URL is created
url_hash = models.CharField(max_length=10,unique=True,db_index=True) # base64 encoded URL id
def _generateShortUrl(self):
"""
This function will generate base64 encoded URL hash
"""
hash = base64.urlsafe_b64encode(uuid.uuid1().bytes)[:6]
hash_exist = short_url.objects.filter(url_hash=hash)
while hash_exist:
hash = base64.urlsafe_b64encode(uuid.uuid1().bytes)[:6]
hash_exist = short_url.objects.filter(url_hash=hash)
continue
return hash
def save(self, *args, **kwargs):
"""
Custom Save method for link model
"""
self.url_hash = self._generateShortUrl()
super(short_url, self).save(*args, **kwargs)
def get_short_url(self):
"""
This method returns the url_hash related to the url
"""
return HOST_NAME + self.url_hash
def get_stats_url(self):
"""
This method returns the stats page URL for a url_hash
"""
return HOST_NAME + self.url_hash + STATS_URI
def __unicode__(self):
"""
This method convert Django model object to the user readable string
"""
return unicode(self.url)
class click_info(models.Model):
"""
This is a click_info class
"""
user_ip = models.TextField() # Store the user_ip
user_agent = models.TextField() # Store the user_agent
http_refrer = models.TextField() # Store the http_refrer
hash = models.ForeignKey(short_url) # base64 encoded URL id
get_parameters = models.TextField() # Store other get_parameters
request_time = models.DateTimeField() # When user made the request_time
updated_at = models.DateTimeField(auto_now=True) # When click_info is updated
created_at = models.DateTimeField(auto_now_add=True) # When click is created
def get_parameters_dict(self):
"""
This method returns the get parameter dict
"""
return ast.literal_eval(self.get_parameters)
def __unicode__(self):
"""
This method convert Django model object to the user readable string
"""
return unicode(self.hash)
#------------------------------------------------------------------------------
class short_url_admin(admin.ModelAdmin):
"""
short_url_admin class
"""
list_display = ('url','blocked','updated_at',
'created_at','url_hash')
exclude = ('url_hash',)
class url_info_admin(admin.ModelAdmin):
"""
url_info_admin class
"""
list_display = ('user_ip','user_agent','http_refrer',
'hash','request_time','get_parameters_dict')
#------------------------------------------------------------------------------
admin.site.register(short_url,short_url_admin)
admin.site.register(click_info,url_info_admin)
做这件事的最好方法是什么?
5条答案
按热度按时间htzpubme1#
我通常更喜欢在管理中执行此操作。这是snippet:
要在视图函数中使用它
guykilcj2#
以下是@symbiotech对
Python 3
的回答的更新版本。实用程序.py
查看次数.py
lymgl2op3#
嗨你可以简单地做
plupiseo4#
调整了一行。为了避免返回AttributeError异常,如果属性不存在,可以添加默认值None。
k2arahey5#
另一个快速简单的方法是使用Pandas图书馆。