我想从单个视图运行多个查询集。我已经完成了将单个get_queryset和单个context_object_name传递到index.html模板的工作:
class IndexView(generic.ListView):
template_name = 'doorstep/index.html'
context_object_name = 'all_hotels'
def get_queryset(self):
return Hotel.objects.all().order_by('rating').reverse()[:3]
Run Code Online (Sandbox Code Playgroud)
现在,我需要Hotel.objects.all().order_by('star').reverse()[:3]
从相同的IndexView运行此
查询集,并将此查询集的context_object_name传递给相同的template_name。
我得到{% for hotel in all_hotels %}了模板中的值
我想插入自动插入created_at日期时间和相应的updated_at日期时间的数据.
我有一个pojo:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@Column(name = "name")
private String name;
@Basic(optional = false)
@CreationTimestamp
@Column(name = "created_at")
@Temporal(TemporalType.TIMESTAMP)
private Date createdAt;
@UpdateTimestamp
@Column(name = "updated_at")
@Temporal(TemporalType.TIMESTAMP)
private Date updatedAt;
Run Code Online (Sandbox Code Playgroud)
在我的数据库中,我使用DATETIME作为created_at和updated_at的类型.但我无法插入数据,而是提供错误消息not-null属性引用null或transient值:com.project.test.entity.Stamp.createdAt.
我想知道为created_at和updated_at DateTime字段定义数据库模式的正确方法,以使其自动生成.