models

Django Order_BY custom function

隐身守侯 提交于 2019-12-23 11:59:10
问题 I have field in model roll_numb . The roll_numb has values as follows. 070-001 070-007 070-343 080-002 080-008 When i order_by roll_numb the sorting is as it is above. I want to split the roll_numb by - and sort by the remainder. (i.e. 001, 002, 008) Code class Meta: ordering = ['roll_numb'] 回答1: I think it is not possible to order a queryset by method of model in Django's ORM scope. So in order to sort your queryset by your custom method, I recommend 2 ways: First qs = mymodel.objects.all()

Best practice for setting default values for model properties in Domain Driven Design?

蓝咒 提交于 2019-12-23 09:17:27
问题 What's the best way to set default properties for new entities in DDD? Also, what's the best way to set default states for complex properties (eg. collections)? My feeling is that default values should be in the models themselves as they are a form of business rule ("by default, we want X's to be Y & Z"), and the domain represents the business. With this approach, maybe a static "GetNew()" method on the model itself would work: public class Person { public string Name { get; set; } public

get model attribute dynamically in rails 3

允我心安 提交于 2019-12-23 07:21:56
问题 How can I get the attribute from the model object dynamically? I have the attribute of the User object as string: u = User.find(1) Can I do something like u.get("user_id") 回答1: You could try using the ActiveRecord model instance like a hash. u = User.find(1) name = u[:name] field = "first_name" first_name = u[field] 回答2: Yet another approach: attr = :first_name @user.read_attribute attr 回答3: Try this user = User.find(1) then something like this should do what you require user.send('field_name

Creating a dynamic path when I upload images

前提是你 提交于 2019-12-23 05:50:13
问题 I'm trying to create a dynamic path for when my users upload images. It works something like this: View: photo = Photo(...) photo.save() photo.original.save(filename, content) Model: album = models.ForeignKey(Album) original = models.ImageField(upload_to="photos/%s/o" % str(album.id), max_length=200) But when I try to do this, Django says no way. Exception Value: 'ForeignKey' object has no attribute 'id' How can I access the model members of a ForeignKey object in this manner? Thanks. 回答1:

How to create model class for Json response from webservice in Swift 3

▼魔方 西西 提交于 2019-12-23 05:13:40
问题 I am new to swift. And I am working on swift project. I have getting response from server response like follows kNetworkManager.executePostRequest(urlString: kAppSocialLoginURL, params:mainDictionary, requestVC: self,completionHandler: {(responseObject) -> () in // print("response object:\(responseObject!)") if responseObject != nil { let responseDictionary = responseObject as! NSDictionary if responseDictionary is NSDictionary{ let obj = responseDictionary.value(forKey:"user") if obj is

How to serialize a GenericRelation in django

狂风中的少年 提交于 2019-12-23 04:40:07
问题 I am using django rest framework and I want to serialize a GenericRelation . In my models, I have: class Asset(model.Models): name = models.CharField(max_length=40) content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey('content_type','object_id') class Project(models.Model): name = models.CharField(max_length=40) file = generic.GenericRelation(Asset) I'm trying to write a serialize for my project which will return

How can change grouped List<MyClass> to list<MyAnotherClass> type?

放肆的年华 提交于 2019-12-23 03:26:12
问题 Here is my codes; public List<SbtKlasorViewModel> GetFoldersWithIndexedDocuments() { //TODO - I can't find right query. We need folders with documents but only documents which be Indexed using (ISession session = DatabaseProvider.SessionFactory.OpenSession()) { List<DokumanlarModel> dokumanList = session.QueryOver<DokumanlarModel>() .Where(x => x.IndexlenmeTarihi != null) .List().ToList(); var list = dokumanList.GroupBy(x => x.Klasor.Aciklama); List<SbtKlasorModel> folders = list as List

Django: Advice on designing a model with varying fields

痞子三分冷 提交于 2019-12-23 03:22:05
问题 I'm looking for some advice/opinions on the best way to approach creating a sort-of-dynamic model in django. The structure needs to describe data for Products. There are about 60 different possible data points that could be relevant, with each Product choosing about 20 of those points (with much overlapping) depending on its ProductType. There are about 2 dozen different ProductTypes, with each ProductType falling into one of 6 categories. The obvious, but ungraceful method, would be to just

Django: Advice on designing a model with varying fields

喜夏-厌秋 提交于 2019-12-23 03:21:11
问题 I'm looking for some advice/opinions on the best way to approach creating a sort-of-dynamic model in django. The structure needs to describe data for Products. There are about 60 different possible data points that could be relevant, with each Product choosing about 20 of those points (with much overlapping) depending on its ProductType. There are about 2 dozen different ProductTypes, with each ProductType falling into one of 6 categories. The obvious, but ungraceful method, would be to just

How do I specify type of index in django? (btree vs hash, etc)

荒凉一梦 提交于 2019-12-22 10:56:18
问题 Like the title says, how do I specify the type of index I want on a field in a model in django. class Person: ... age = models.IntegerField(db_index=True) But now what? How do I make sure it is a btree index and not a hash . Or is this all done automatically for us and there is some large table that django uses for choosing the "optimal index type" 回答1: Django defaults to creating btree indexes whenever you specify index=True : https://docs.djangoproject.com/en/1.11/ref/models/indexes/ I note