django-models

JSON data convert to the django model

无人久伴 提交于 2019-12-31 23:06:08
问题 I need to convert JSON data to django model. This is my JSON data { "data": [ { "id": "20ad5d9c-b32e-4599-8866-a3aaa5ac77de", "name": "name_1" }, { "id": "7b6d76cc-86cd-40f8-be90-af6ced7fec44", "name": "name_2" }, { "id": "b8843b1a-9eb0-499f-ba64-25e436f04c4b", "name": "name_3" } ] } This is my django method def get_titles(): url = 'http://localhost:8080/titles/' r = requests.get(url) titles = r.json() print(titles['data']) What I need is convert to the model and pass to the template. Please

Django Table with Million of rows

陌路散爱 提交于 2019-12-31 22:25:23
问题 I have a project with 2 applications ( books and reader ). Books application has a table with 4 milions of rows with this fields: book_title = models.CharField(max_length=40) book_description = models.CharField(max_length=400) To avoid to query the database with 4 milions of rows, I am thinking to divide it by subject ( 20 models with 20 tables with 200.000 rows ( book_horror, book_drammatic, ecc ). In "reader" application, I am thinking to insert this fields: reader_name = models.CharField

Get django object id based on model attribute

霸气de小男生 提交于 2019-12-31 17:58:35
问题 I have a basic model named "Places" which has this view: def view_index(request, place_name): The user will access that view with a URL like this one: http://server.com/kansas "kansas" is a value stored in a field named "name" inside the model "Places". The problem is that I can't figure out how to obtain the object id based just on the object name. Is there a way to do this? 回答1: Like this: place = Places.objects.get(name='kansas') print place.id 回答2: Since you only want id , you should only

Get django object id based on model attribute

我们两清 提交于 2019-12-31 17:57:50
问题 I have a basic model named "Places" which has this view: def view_index(request, place_name): The user will access that view with a URL like this one: http://server.com/kansas "kansas" is a value stored in a field named "name" inside the model "Places". The problem is that I can't figure out how to obtain the object id based just on the object name. Is there a way to do this? 回答1: Like this: place = Places.objects.get(name='kansas') print place.id 回答2: Since you only want id , you should only

Django annotate query set with a count on subquery

≡放荡痞女 提交于 2019-12-31 12:54:04
问题 This doesn't seem to work in django 1.1 (I believe this will require a subquery, therefore comes the title) qs.annotate(interest_level= \ Count(Q(tags__favoritedtag_set__user=request.user)) ) There are items in my query set which are tagged and tags can be favorited by users, I would like to calculate how many times a user had favorited each item in the set via tags. is there a way to construct a query like this without using extra()? Thanks. 回答1: Looking at the add_aggregate function within

SQLAlchemy Model Django like Save Method?

耗尽温柔 提交于 2019-12-31 10:58:12
问题 I am using sqlalchemy for a project. However, I am more accustomed to Django's ORM. I would like to know if, in the sqlachemy ORM, there is anything similar to a Django models' save() method that I can overrride to implement actions automatically upon a 'commit' / 'save.' 回答1: You can extend your models with some simple crud methods to achieve something similar to Django ORM / ActiveRecord: # SQLAlchemy db_session setup omitted ... Model = declarative_base(name='Model') Model.query = db

Django set Storage Engine & Default Charset

天涯浪子 提交于 2019-12-31 10:47:11
问题 Creating my tables from my models.py . I donno how to do 2 things - I want to specify MySQL to create some of my tables as InnoDB & some as MyISAM . How do I do it? Also I want to specify my tables DEFAULT CHARSET as utf8 . How do I do it? This is what I see when I run syncdb - ... ) ENGINE=MyISAM DEFAULT CHARSET=latin1 I use Ubuntu 10.04, Django 1.2.X, MySQL 5.1.X UPDATE : I thought these might be MySQL default settings & I ended up changing my.cnf where I added default-character-set = utf8

Django model with FileField — dynamic 'upload_to' argument

佐手、 提交于 2019-12-31 10:43:38
问题 I am using the model with FileField to deal with file uploading. Now the files can be uploaded successfully. However, there is one more small improvement I want to make, which is to create folder for the user with the username. Here is the code I've tried class UserFiles(models.Model): user = models.OneToOneField(User) file = models.FileField(upload_to='files/users/user.username/%Y_%m_%d/') this would give the folder of 'user.username' instead of 'John'(one example of username) I have also

Django model with FileField — dynamic 'upload_to' argument

▼魔方 西西 提交于 2019-12-31 10:42:18
问题 I am using the model with FileField to deal with file uploading. Now the files can be uploaded successfully. However, there is one more small improvement I want to make, which is to create folder for the user with the username. Here is the code I've tried class UserFiles(models.Model): user = models.OneToOneField(User) file = models.FileField(upload_to='files/users/user.username/%Y_%m_%d/') this would give the folder of 'user.username' instead of 'John'(one example of username) I have also

Django Class Based View for both Create and Update

倾然丶 夕夏残阳落幕 提交于 2019-12-31 09:11:50
问题 Say I want to create a Class Based View which both updates and creates an object. From a previous question I worked out I could do one of the following things: 1) Use 2 generic views CreateView and UpdateView which I think would mean having two URL's pointing to two different classes. 2) Use a class based view which inherits base View , which I think would mean having two URL's pointing to just 1 class (I created which inherits View ). I have two questions: a) Which is better? b) ccbv.co.uk