django-models

Model.save() not called on update()

半腔热情 提交于 2020-01-05 07:20:07
问题 I added a save() method to my Model , to update some timestamps: class Order(models.Model): deliveredtime = models.DateTimeField(blank=True, null=True, default=None) status = models.CharField(default='NEW', max_length=20) def save(self, *args, **kw): if self.status == "DELIVERED" and self.deliveredtime is None: self.deliveredtime = timezone.now() super(Order, self).save(*args, **kw) But I found out this method is not called when calling update on a list of objects: Order.objects.filter(status

Use Django's models in a Scrapy project (in the pipeline)

半世苍凉 提交于 2020-01-05 07:11:08
问题 This has been asked before but the answer that always comes up is to use DjangoItem. However it states on it's github that: often not a good choice for a write intensive applications (such as a web crawler) ... may not scale well This is the crux of my problem, I'd like to use and interact with my django model in the same way I can when I run python manage.py shell and I do from myapp.models import Model1 . Using queries like seen here. I have tried relative imports and moving my whole scrapy

How do you bind a model to two other, already related models, and display the information?

风格不统一 提交于 2020-01-05 05:56:19
问题 Apologies if the title is not up to scratch, I wasn't sure how to word my problem. Please feel free to edit in a better one. Here is what I am trying to do: I am trying to create a relational database using a few different models. The idea is: A contract is unique Any contract can have (and share) any number of projects Unique information needs to be stored about each specific project for a specific contract So far, the first two bullet points I have working as expected. The third point seems

django model and sqlite db creation bug

ぃ、小莉子 提交于 2020-01-05 05:26:35
问题 I have the following model. from django.db import models class Client(models.Model): postcode = models.CharField(max_length=10) first_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100) address = models.TextField(blank=True) phone = models.IntegerField(blank=True) email = models.EmailField(blank=True) url = models.URLField(blank=True) client_since = models.DateTimeField('Client Since') def __unicode__(self): return self.first_name def client_since(self):

Does the ORM call init when retrieving a saved instance?

你说的曾经没有我的故事 提交于 2020-01-05 04:11:31
问题 I'm doing a migration changing a nullable field to be not-nullable. The new __init__ routine ensures that fields can't be null by doing some custom callisthenics to come up with a suitable default. The question is whether it is essential to migrate the existing data to apply the new rules for a default, or will those rules be applied automagically whenever a legacy object is retrieved? Reading the source I suspect the ORM restores a pickle of the saved data, thus I would need to update all

Python - Django signup for custom user model with multiple types of users

心已入冬 提交于 2020-01-05 04:09:08
问题 I'm working on a project using Python(3.7) and Django(2.2) in which I have created multiple types of users with a custom user model by extending the AbstractBaseUser model. I have done with the login but I'm confused about how should I implement the signup page as there's multiple types of users and I need a different form for each type of user. Here's How I have implemented my models. From models.py : class UserManager(BaseUserManager): def _create_user(self, email, password, is_staff, is

Images in quiz model

。_饼干妹妹 提交于 2020-01-05 04:08:22
问题 I'm trying to customise this django quiz app I want that it can be possible to add images for every question and every answer. But some will not have any images, either answers. What is the best way to do this? This is the actual model: class Question(models.Model): quiz = models.ManyToManyField(Quiz, blank=True, ) category = models.ForeignKey(Category, blank=True, null=True, ) content = models.CharField(max_length=1000, blank=False, help_text="Enter the question text that you want displayed"

SELF JOIN queryset on joined table in Django?

纵然是瞬间 提交于 2020-01-05 04:07:22
问题 I want to make a self join on an joined table. I have a queryset: paymentsss = Transaction.objects.all().select_related('currency', 'payment_source__payment_type','deal__service', 'deal__service__contractor').order_by('-id') Which is converted to: SELECT "processing"."transaction"."id", "processing"."transaction"."currency_id", "processing"."transaction"."deal_id", "processing"."transaction"."payment_source_id", "processing"."transaction"."payment_date", "processing"."transaction"."amount",

Django: OperationalError when I try to create user from admin

a 夏天 提交于 2020-01-05 04:04:11
问题 I have Django 1.10 with MySQL, every time I enter admin and I try to make a user I get this error: The thing is I can create any other model and if I create a super user from command line I can edit that user but when I press "add user" I get this error. I tried deleting the database and making it again twice. And here is my settings.py: import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) DEBUG = True ALLOWED_HOSTS = [] INSTALLED_APPS = [ # django apps 'django

Django: ValueError when saving an instance to a ForeignKey Field

佐手、 提交于 2020-01-05 03:51:18
问题 I am trying to save an instance of a model but get a ValueError. ValueError: Cannot assign "<DataPointModel: DataPointModel object>": "Bike.data_point" must be a "DataPointModel" instance. The model has a very simple field: data_point = models.ForeignKey(DataPointModel, null=True, blank=True) And the method is as well. data_point = DataPointModel.objects.filter( object_id=bike.reference_model.id, ).last() watch.data_point = data_point watch.save() Why is this not saving? 回答1: Whenever you