我对date_joined字段使用基本的用户模型。
我已经在Google搜索中做了几乎所有的工作。
views.py
def list(self, request, *args, **kwargs):
form = ModelInfoForm(request.GET or None)
serializer = UserSerializer(self.queryset, many=True)
return Response({'users':serializer.data, 'form':form}, template_name = 'user/userinfo.html')
serializers.py
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('username', 'email', 'first_name', 'last_name', 'is_superuser', 'is_staff', 'is_active', 'date_joined',
'get_absolute_url')
settings.py
USE_L10N = False
USE_TZ = False
DATETIME_FORMAT = '%d-%m-%Y-%H-%M-%S'
template.html
<td>{{ user.date_joined }}</td>
<td>{{ user.date_joined|date:"d:m:Y H:i:s" }}</td>
我得到了这样的结果
{{ user.date_joined }}
=> 2018-06-14T12:02:21
{{ user.date_joined|date:"d:m:Y H:i:s" }}
=> (nothing)
{{ user.date_joined|date:"DATETIME_FORMAT" }}
=> (nothing)
{{ user.date_joined|date:DATETIME_FORMAT }}
=> (error)
我还将用户解决方案用于其他模型。
models.py
class TimeStampedModel(models.Model):
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
答案 0 :(得分:0)
您可以在输出格式中为joined_date指定格式参数。
class UserSerializer(serializers.ModelSerializer):
date_joined = serializers.DateTimeField(format="<Your format>")
class Meta:
model = User
fields = ('username', 'email', 'first_name', 'last_name', 'is_superuser', 'is_staff', 'is_active','get_absolute_url')
答案 1 :(得分:0)
我用自定义的DATETIME_FORMAT解决了
settings.py
DATETIME_FORMAT = '%Y-%m-%d %H:%m'
serializers.py
class UserSerializer(serializers.ModelSerializer):
date_joined = serializers.DateTimeField(format=base.DATETIME_FORMAT, input_formats=None)
class Meta:
model = User
fields = ('username', 'email', 'first_name', 'last_name', 'is_superuser', 'is_staff', 'is_active', 'date_joined',
'get_absolute_url', 'pk')