首页 \ 问答 \ 模型字段(在django中)和序列化器字段之间的区别(在django rest框架中)(Difference between model fields(in django) and serializer fields(in django rest framework))

模型字段(在django中)和序列化器字段之间的区别(在django rest框架中)(Difference between model fields(in django) and serializer fields(in django rest framework))

由于我们可以使用传统模型字段验证值,因此Django REST Framework包含其自己的序列化器字段。 我知道序列化器字段用于处理原始值和内部数据类型之间的转换。 除此之外,它们之间有什么不同。


As we can validate the values using the conventional model field then why Django REST Framework contains its own serializer fields. I know that serializer fields are used to handle the converting between primitive values and internal datatypes. Except this, is there anything different between them.


原文:https://stackoverflow.com/questions/35129697
更新时间:2023-05-21 19:05

最满意答案

您可以按月和按年对TimeSheet记录进行分组,如下所示:

DateTimeFormatInfo dtfi = new DateTimeFormatInfo();

// gets the records grouped based on Year and Month. 
// Creates an object of IEnumerable<IGrouping<>> type
var groupedByMonth = result
                        .OrderByDescending(x => x.Date)
                        .GroupBy(x => new { x.Date.Year, x.Date.Month }).ToList();


// gets the months names in a list
List<string> monthNames = groupedByMonth
                    .Select(a => dtfi.GetAbbreviatedMonthName(a.Key.Month))
                    .ToList();

// gets the total hours per month
List<int> hoursPerMonth = groupedByMonth
                        .Select(a => a.Sum(p => p.Hours))
                        .ToList();


ArrayList xValue = new ArrayList(monthNames);
ArrayList yValue = new ArrayList(hoursPerMonth);

我正在使用DateTimeFormatInfo来获取月份的名称。 没有这个你也可以实现这个目标:

List<string> monthNames = groupedByMonth
                    .Select(a => a.FirstOrDefault().Date.ToString("MMMM"))
                    .ToList();

You can group the TimeSheet records by month and year like this:

DateTimeFormatInfo dtfi = new DateTimeFormatInfo();

// gets the records grouped based on Year and Month. 
// Creates an object of IEnumerable<IGrouping<>> type
var groupedByMonth = result
                        .OrderByDescending(x => x.Date)
                        .GroupBy(x => new { x.Date.Year, x.Date.Month }).ToList();


// gets the months names in a list
List<string> monthNames = groupedByMonth
                    .Select(a => dtfi.GetAbbreviatedMonthName(a.Key.Month))
                    .ToList();

// gets the total hours per month
List<int> hoursPerMonth = groupedByMonth
                        .Select(a => a.Sum(p => p.Hours))
                        .ToList();


ArrayList xValue = new ArrayList(monthNames);
ArrayList yValue = new ArrayList(hoursPerMonth);

I'm using DateTimeFormatInfo to get the month's name. You can also achieve this without this:

List<string> monthNames = groupedByMonth
                    .Select(a => a.FirstOrDefault().Date.ToString("MMMM"))
                    .ToList();

相关问答

更多
  • 那么在格式化的情况下, DisplayFormat属性可能是一个很好的解决方案: [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")] public DateTime Date { get; set; } 然后简单地: @Html.DisplayFor(x => x.Date) 就截断字符串而言,自定义HTML帮助程序是一个很好的解决方案。 更新: 关于你的编辑,一个自定义的HTML帮手可能在这种情况下工作,但也有一个我非常喜欢的替代方法:查看模型。 ...
  • 要在RC2版本中设置日期区域设置,可以在Configure方法中的Startup.cs文件中使用以下内容: var ci = new CultureInfo("en-GB"); ci.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy"; // Configure the Localization middleware app.UseRequestLocalization(new RequestLocalization ...
  • 你可以这样打电话给你的帮手: @{ Html.IncrementalMenu(MenuBlock.Site); } WebForms语法 <% Html.IncrementalMenu(MenuBlock.Site); %> 你只需调用你的方法,并返回值(如果有的话)被忽略。 像这样的代码期望返回值,并将返回值写入到html流中: @Html.YourHelper() Webforms语法: <%: Html.YourHelper() %> 同样的,如果结果值!= IHtmlString: <%= ...
  • 一个选项 - 这是来自Telerik的 - 是将日期作为字符串从服务器而不是.Net DateTime对象传递。 客户端将像解析日期对象一样解析它。 使用此格式“yyyy-mm-dd HH:MM” 所以你的json会是这样的: [ {"WeekofYear":45,"Value":96.08,"WeekBeginDate":"2014-12-16 07:00"}, ... An option - this is comming from Telerik - is to pass the d ...
  • 这样的事情会让你前进: