django-models

Django partial update

ぐ巨炮叔叔 提交于 2021-01-03 08:36:27
问题 I have two threads, one which runs something like update t set ColA=foo and the other runs update t set ColB=foo . If they were doing raw SQL statements there would be no contention, but since Django gets and saves the entire row, a race condition can occur. Is there any way to tell Django that I just want to save a certain column? 回答1: You are correct that save will update the entire row but Django has an update which does exactly what you describe. https://docs.djangoproject.com/en/stable

python/django: model object has no attribute 'prefetch_related'

青春壹個敷衍的年華 提交于 2021-01-03 06:50:25
问题 I have created a model 'VehicleDetails' in which a user can fill the details of a vehicle and another model 'TripStatus' in which he updates the vehicle location. I wanted to get the latest location for which i did as in my below code. I use prefetch_related in my view to returns the location values for a particular vehicle. But, when after running the server, it raises an error : "TripStatus object has no attribute 'prefetch_related'". I would appreciate helping me solve this. models.py:

Why does Django's URLField truncate to 200 characters by default?

一曲冷凌霜 提交于 2021-01-03 06:36:50
问题 I'm fond of Django and use it regularly. I find most of its defaults sane, but one has always bothered me to the point that I override it on every project. The default max length of a URLField model field is 200 characters. The docs verify this limit, but don't explain why it is the case. class URLField(CharField): ... def __init__(self, verbose_name=None, name=None, **kwargs): kwargs['max_length'] = kwargs.get('max_length', 200) ... I override it because plenty of URLs are longer than 200

Django Mass Update/Insert Performance

半腔热情 提交于 2021-01-02 09:06:16
问题 I'm receiving financial data for approximately 5000 instruments every 5 seconds, and need to update the respective entries in the database. The model looks as follows: class Market(models.Model): market = models.CharField(max_length=200) exchange = models.ForeignKey(Exchange,on_delete=models.CASCADE) ask = models.FloatField() bid = models.FloatField() lastUpdate = models.DateTimeField(default = timezone.now) What needs to happen is the following: After new financial data is received, check if

Django somehow cannot determine simple annotate function

与世无争的帅哥 提交于 2021-01-01 08:20:14
问题 I'm trying to create an annotation on a queryset class that simply adds a boolean that is the result of some standard queries. CustomQueryset(models.QuerySet): """ An extension of the traditional queryset to support filtering on accepting_offers """ def annotate_with_accepting_offers(self): """ Add a lovely little variable to the SELECT that says if the listing is accepting offers. A <thing> is accepting offers when its: + not cancelled + expire date is today or in the future + has spaces

CircularDependencyError on Foreign Keys Pointing to Each Other Django

巧了我就是萌 提交于 2021-01-01 07:55:30
问题 I get a circular dependency error that makes me have to comment out a field. Here is how my models are set up: class Intake(models.Model): # This field causes a bug on makemigrations. It must be commented when first # running makemigrations and migrate. Then, uncommented and run makemigrations and # migrate again. start_widget = models.ForeignKey( "widgets.Widget", on_delete=models.CASCADE, related_name="start_widget", null=True, blank=True, ) class Widget(PolymorphicModel): intake = models

CircularDependencyError on Foreign Keys Pointing to Each Other Django

百般思念 提交于 2021-01-01 07:54:54
问题 I get a circular dependency error that makes me have to comment out a field. Here is how my models are set up: class Intake(models.Model): # This field causes a bug on makemigrations. It must be commented when first # running makemigrations and migrate. Then, uncommented and run makemigrations and # migrate again. start_widget = models.ForeignKey( "widgets.Widget", on_delete=models.CASCADE, related_name="start_widget", null=True, blank=True, ) class Widget(PolymorphicModel): intake = models

CircularDependencyError on Foreign Keys Pointing to Each Other Django

做~自己de王妃 提交于 2021-01-01 07:52:25
问题 I get a circular dependency error that makes me have to comment out a field. Here is how my models are set up: class Intake(models.Model): # This field causes a bug on makemigrations. It must be commented when first # running makemigrations and migrate. Then, uncommented and run makemigrations and # migrate again. start_widget = models.ForeignKey( "widgets.Widget", on_delete=models.CASCADE, related_name="start_widget", null=True, blank=True, ) class Widget(PolymorphicModel): intake = models

Django - form_valid() vs save()

眉间皱痕 提交于 2020-12-30 07:40:10
问题 In django forms, for saving other data I usually use form_valid() but as I can also use save() method of formclass. Today I overrided save() instead of form_valid() and I got problem with my manytomanyfield. When using , the values of manytomanyfield are not saved but when I use form_valid() they start saving. Can anybody tell me the reason and what are the differences between both, which is the most convenient method and in what situation? Here is my overriding of save() method: class

Auto increment django model field per user

…衆ロ難τιáo~ 提交于 2020-12-30 06:36:05
问题 I have this model: class Invoice(models.Model): owner = models.ForeignKey(settings.AUTH_USER_MODEL) data = models.TextField(default=None, blank=True, null=True) number = models.PositiveIntegerField(default=0, null=False) What I need is to auto-increment the field number for each separated user. The rationale is that each user has a list of Invoice , starting from number=1 to number=latest.number+1 . I do known about F() expressions, but can't figure out how to reference the latest/greatest