默认情况下,Django DateTime归档保存并显示秒和毫秒以及数据中的额外信息,例如,它保存并显示日期,例如:
2018-07-21 05:27:29.736956+00:00
在尝试检索数据时,是否有任何方法或任何全局函数来忽略额外的信息并仅获取日期和时间?像
2018-07-21 05:27:29
型号:
class Enrolled(models.Model):
user = models.ForeignKey(User, on_delete=models.DO_NOTHING)
product = models.ForeignKey(Product, on_delete=models.CASCADE, to_field='product_id',
related_name='enroll')
created_date = models.DateTimeField(auto_now_add=True)
serilizer
class EnrolledSerializer(ModelSerializer):
ptitle = serializers.SerializerMethodField()
instructor = serializers.SerializerMethodField()
instructorid = serializers.SerializerMethodField()
pslug = serializers.SerializerMethodField()
def get_instructor(self, object):
return object.product.author.username
def get_instructorid(self, object):
return object.product.author.id
def get_ptitle(self, obj):
return obj.product.title
def get_pslug(self, obj):
return obj.product.slug
class Meta:
model = Enrolled
fields = [
'id',
'user',
'product',
'ptitle',
'instructorid',
'pslug',
'instructor',
'created_date',
]
read_only_fields = ['product', 'created_date', 'id', 'user', 'instructor', 'instructorid', 'pslug', 'ptitle']
//编辑:
REST_FRAMEWORK = {
'DATETIME_FORMAT': ("%Y-%m-%d %H:%M"),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.AllowAny',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
),
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
)
}
USE_I18N = False
USE_L10N = False
USE_TZ = True
答案 0 :(得分:0)
似乎与Change time format in Django Views
有关使用
override func viewDidAppear(_ animated: Bool) {
if API.User.CURRENT_USER != nil {
// segue to the Tab Bar Controller
self.performSegue(withIdentifier: "signInToTabBar", sender: nil)
}
super.viewDidAppear(true)
self.tabBarController?.tabBar.isHidden = false
}
或者,您可以按照模板过滤器https://docs.djangoproject.com/en/2.0/ref/templates/builtins/#std:templatefilter-date
的格式设置日期时间。microsecond=0
答案 1 :(得分:0)
您可以通过设置所有日期时间字段来为static void Main(string[] args)
{
int[] a = { 1, 2, 3, 9 };
int[] b = { 4, 7, 2 };
InsertElements(a, b, 3, 2);
}
private static void InsertElements(int[] arr1, int[] arr2, int x, int n)
{
var listOfArr1 = arr1.ToList();
var itemsToInsert = arr2.Take(n);
var itemsToAdd = arr2.Skip(n);
var pos = x - 1;
foreach (var item in itemsToInsert)
{
listOfArr1.Insert(pos, item);
pos++;
}
listOfArr1.AddRange(itemsToAdd);
var result = listOfArr1.ToArray();
}
设置日期时间格式:
created_date
仅适用于当前的序列化程序,如果您的其他字段仅供参考,则可以使用REST_FRAMEWORK = {
'DATETIME_FORMAT': "%Y-%m-%d %H:%M",
}
选项,例如:
source
阅读文档:date-and-time-fields和source。