FluentNhibernate,检索部分对象图(FluentNhibernate, retrieve partial Object Graph)
因此,我将使用FluentNHibernate调用存储库来检索复杂对象图的根对象。 但对于某些子级别对象,我不想检索所有元素,而只检索日期参数等于某些条件的元素。 在下面的代码中,我希望OrderTime字段以这种方式过滤较低级别的Order对象。
含义我想检索所有用户的所有用户组,但每个用户的订单对象只包含来自特定日期或日期范围的订单。
那么我对如何检索这个对象图有什么选择呢? 我不想延迟加载,我只想指定一些不同的检索条件,这些条件永远不会改变。 所以它们可以是存储库的独立功能,就像最后建议的那样。 但是,我将如何编写这些方法,如何指定这些条件?
对象:
public class UserGroup { public int Id; public IList<User> Users; } public class User { public int Id; public string Name; public IList<Order> Orders; } public class Order { public int Id; public decimal Price; public System.DateTime OrderTime; }
库:
public class UserGroupRepository { public List<UserGroup> GetAll() { using (ISession session = FNH_Manager.OpenSession()) { dynamic obj = session.CreateCriteria(typeof(UserGroup)).List<UserGroup>(); return obj; } } }
潜在的新存储库方法:
public List<UserGroup> GetAll_FilterOrderDate(System.DateTime _date) { } public List<UserGroup> GetAll_FilterOrderDate(List<System.DateTime> _dates) { }
So I will call a repository to retrieve the root object of a complex object graph, using FluentNHibernate. But for some sub-level objects I don't want to retrieve all elements, but only those where a date parameter equals certain condition. In below code, I want the lower level Order object to be filtered in this way by the OrderTime field.
Meaning I want to retrieve all UserGroups, with all Users, but the Orders object of each User shall only contain orders from a specific date or date range.
So what are my options on how to retrieve this object graph? I don't want lazy loading, I just want to specify a handful of different retrieval conditions, which will never change. So they can be separate functions of the repository, like suggested at the end. But how would I go about coding those methods, how to specify these conditions?
Objects:
public class UserGroup { public int Id; public IList<User> Users; } public class User { public int Id; public string Name; public IList<Order> Orders; } public class Order { public int Id; public decimal Price; public System.DateTime OrderTime; }
Repository:
public class UserGroupRepository { public List<UserGroup> GetAll() { using (ISession session = FNH_Manager.OpenSession()) { dynamic obj = session.CreateCriteria(typeof(UserGroup)).List<UserGroup>(); return obj; } } }
Potential new Repository methods: ?
public List<UserGroup> GetAll_FilterOrderDate(System.DateTime _date) { } public List<UserGroup> GetAll_FilterOrderDate(List<System.DateTime> _dates) { }
原文:https://stackoverflow.com/questions/7071291
最满意答案
在python中,您可以使用name属性获取。
newdoc = request.FILES['docfile'] newdoc.name # file name
Andv如果你希望在模板中看到这里是代码
{% for document in documents %} {{ document.file.filename }} {% endfor }}
在模型对象中
newdoc = Document.objects.get(id=1) newdoc.docfile.url # url of the file newdoc.docfile.path # exact path
In python you can get that using name attribute.,.
newdoc = request.FILES['docfile'] newdoc.name # file name
Andv if you wish to see that in the template here is the code
{% for document in documents %} {{ document.file.filename }} {% endfor }}
In the model objects
newdoc = Document.objects.get(id=1) newdoc.docfile.url # url of the file newdoc.docfile.path # exact path
相关问答
更多-
Django URL结束(Django URL endswith)[2023-07-08]
问题出在您的设置文件中: STATIC_URL或MEDIA_URL有一个尾随逗号,将其变成一个元组。 The problem is in your settings file: one of STATIC_URL or MEDIA_URL has a trailing comma, which turns it into a tuple. -
as_view()方法返回你需要用request参数调用的视图函数: return ListView.as_view(.....)(request) as_view() method returns view function which you need to call with request argument: return ListView.as_view(.....)(request)
-
我会说你有一个slug=""的东西。 你可以使用django shell来检查这个: from yourapp.models import Thing Thing.objects.get(slug='') 根据你的模型定义, slug可以是空白的,但你的url模式不接受空白slug。 你将不得不修复你的slug字段或你的url模式。 Finally figured it out. The problem was nowhere in the code. Actually all I needed was ...
-
FullCalendar和django(FullCalendar and django)[2022-03-25]
解决了它。 我想fullcalendar想要一个包含json对象的列表,但我正在返回普通的json对象。 我做的是 for entry in entries: id = entry.id title = entry.title start = entry.date.strftime("%Y-%m-%dT%H:%M:%S") allDay = False json_entry = {'id':id, 'start':start, 'allDay':allDay, 't ... -
您的Javascript中存在拼写错误。 它应该是{% csrf_token %}而不是{{ csrf_token }} 。 编辑:在您发表评论后,我仔细查看了您链接的文章。 您需要包含库fileuploader.js 。 它将使用带有正确事件处理程序的表单的id file-uploader替换占位符div。 用纯HTML创建表单不起作用。 我建议您查看Github存储库中的示例: https : //github.com/alexkuhl/file-uploader/tree/master/client ...
-
获取Django本身的路径(Get the path to Django itself)[2020-02-15]
只要Django已经在Python进程中import (例如,如果你的代码在视图函数中,它已经被import ),再次导入它将不会做“任何事情”* - 所以坚持下去,使用import django; django.__file__ import django; django.__file__ 。 现在,如果当前的Python进程没有导入Django(例如,你正在调用os.system("myscript.py")而myscript.py需要确定Django的路径),那么import django 会有点浪费 ... -
使用django上传文件(uploading a file using django)[2021-12-22]
在您的视图功能中 def pythonhandler(request): data = DocumentForm(request.POST, request.FILES) 并在您的HTML文件中 -
在python中,您可以使用name属性获取。 newdoc = request.FILES['docfile'] newdoc.name # file name Andv如果你希望在模板中看到这里是代码 {% for document in documents %} {{ document.file.filename }} {% endfor }} 在模型对象中 newdoc = Document.objects.get(id=1) newdoc.docfile.url # url of the fi ...
-
Django谷歌可疑行动(Django Google Suspicious Operation)[2020-09-02]
来自183.91.14.60(REMOTE_ADDR)的某人正在连接到您的服务器并要求Google的主页(REQUEST_URI); 因为你没有托管谷歌这确实是可疑的。 这与Google索引机器人无关。 我也从这个IP地址在我的服务器上看到了这个请求(但不是这个错误消息)。 我的猜测是有人在扫描寻找开放代理的服务器。 我不会将www.google.com添加到任何允许的主机列表中。 如果您从同一个REMOTE_ADDR收到大量这些内容,我会考虑将该IP地址添加到/etc/hosts.deny或防火墙上的阻止 ... -
django有问题(Something wrong with django)[2020-09-01]
我认为你的问题是你试图访问一个post而不迭代posts 。 正如您在post_list视图中看到的post_list ,您有: ... return render(request, 'blog/post_list.html', {'posts': posts}) 因此,您将名为posts的变量发送到模板。 但是在您的模板中,您尝试访问post :{{ post.title }}
但它不 ...