django-models

How to solve SQLdecode error when you migrate models in django?

拥有回忆 提交于 2020-01-13 06:49:53
问题 I am new to django, I have created a project and app and I would like to connect my project to the mongodb. when I enter python manage.py migrate command , I am getting below mentioned error. I have dropped database and cleared all migrations in the django_migration table and deleted migration files in the created migrations folder. Still getting same error. Please help me with this. Thanks in advance Error: raise TypeError("documents must be a non-empty list") TypeError: documents must be a

Django Admin - how to prevent deletion of some of the inlines

醉酒当歌 提交于 2020-01-13 06:35:16
问题 I have 2 models - for example, Book and Page. Page has a foreign key to Book. Each page can be marked as "was_read" (boolean), and I want to prevent deleting pages that were read (in the admin). In the admin - Page is an inline within Book (I don't want Page to be a standalone model in the admin). My problem - how can I achieve the behavior that a page that was read won't be deleted? I'm using Django 1.4 and I tried several options: Override "delete" to throw a ValidationError - the problem

Django manager for _set in model

蓝咒 提交于 2020-01-13 04:39:06
问题 I'm in the progress of learning Django at the moment but I can't figure out how to solve this problem on my own. I'm reading the book Developers Library - Python Web Development With Django and in one chapter you build a simple CMS system with two models (Story and Category), some generic and custom views together with templates for the views. The book only contains code for listing stories, story details and search. I wanted to expand on that and build a page with nested lists for categories

Django - Are model save() methods lazy?

夙愿已清 提交于 2020-01-12 17:22:06
问题 Are model save() methods lazy in django? For instance, at what line in the following code sample will django hit the database? my_model = MyModel() my_model.name = 'Jeff Atwood' my_model.save() # Some code that is independent of my_model... model_id = model_instance.id print (model_id) 回答1: It does not make much sense to have a lazy save, does it? Django's QuerySets are lazy, the model's save method is not. From the django source: django/db/models/base.py , lines 424–437: def save(self, force

MultiValueDictKeyError generated in Django after POST request on login page

两盒软妹~` 提交于 2020-01-12 07:37:07
问题 I'm trying to build a login page. I'm running Django 1.6.1. I've largely been following the tutorial at www.fir3net.com/Django/django.html. For convenience I will repost a lot of it here. Error Message: Request Method: GET Request URL: http://127.0.0.1:8000/login/ Database In Use: SQLite3 Django Version: 1.6.1 Python Version: 2.7.4 Installed Applications: ('django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages',

Django gives “I/O operation on closed file” error when reading from a saved ImageField

左心房为你撑大大i 提交于 2020-01-12 07:26:47
问题 I have a model with two image fields, a source image and a thumbnail. When I update the new source image, save it and then try to read the source image to crop/scale it to a thumbnail I get an "I/O operation on closed file" error from PIL. If I update the source image, don't save the source image, and then try to read the source image to crop/scale, I get an "attempting to read from closed file" error from PIL. In both cases the source image is actually saved and available in later request

Django query how to write: WHERE field LIKE '10__8__0__'?

假装没事ソ 提交于 2020-01-12 07:01:16
问题 There is contains method, but it means: WHERE field LIKE '%10%' . And I need exactly WHERE field LIKE '10__8__0__' . Thanks! EDIT: I mean string is '10xx8xx0xx' , where x - any symbol 回答1: You can do this with django-like: MyModel.objects.filter(field__like='10%8%0%') Also I created a related ticket on the Django project site 回答2: The easiest way to do the search you intend to do is by regular expression: MyModel.objects.filter(field__regex=r'^10..8..0..$') Edit: My solution is possibly much

django convert model instance to dict

最后都变了- 提交于 2020-01-12 07:00:09
问题 I am a beginner in Django. And I need to convert Model instance in to dictionary similar as Model.objects.values do, with relation fields. So I write a little function to do this: def _get_proper(instance, field): if field.__contains__("__"): inst_name = field.split("__")[0] new_inst = getattr(instance, inst_name) next_field = "__".join(field.split("__")[1:]) value = _get_proper(new_inst, next_field) else: value = getattr(instance, field) return value def instance_to_dict(instance, fields):

Django pass object from view to next for processing

会有一股神秘感。 提交于 2020-01-12 05:25:17
问题 If you have 2 views, the first uses modelform that takes inputted information from the user (date of birth, name, phonenumber, etc), and the second uses this information to create a table. How would you pass the created object in the first view to the next view so you can use it in the second view's template I'd appreciate any help you can share 回答1: One approach is to put the object into a session in your first view, which you could then retrieve from the request.session in the second view.

Django - Multiple User Profiles

感情迁移 提交于 2020-01-11 14:48:53
问题 Initially, I started my UserProfile like this: from django.db import models from django.contrib.auth.models import User class UserProfile(models.Model): user = models.OneToOneField(User) verified = models.BooleanField() mobile = models.CharField(max_length=32) def __unicode__(self): return self.user.email Which works nicely along with AUTH_PROFILE_MODULE = 'accounts.UserProfile' set in settings.py . However, I have two different kinds of users in my website, Individuals and Corporate, each