django-models

Django: Sum the count of a sub sub foreign object

一曲冷凌霜 提交于 2020-01-01 17:16:26
问题 I have many hits by Twavail I have many Twavail for a Twaffic I would like to sum the number of Hits for a Twaffic, and get an additionel field in Twaffic entries (hits_count), but I have some difficulties. Here are my models: class Twaffic(models.Model): user = models.ForeignKey(User,editable=False) enchere = models.PositiveIntegerField(default=1) class Twavail(models.Model): user = models.ForeignKey(User,editable=False) twaffic = models.ForeignKey(Twaffic,editable=False,related_name=

Model Formset - By Default model formset is rendering one extra field (2 fields in total)

随声附和 提交于 2020-01-01 14:45:13
问题 My model formset without even defining "extra" parameters in modelformset_factory is rendering one extra field in the template. I have tried many variations but it didn't work. If I print the form (the model form) on the command line it just prints a single form field as required but on model formset it prints 2 by default. Here is my code. models.py class Direction(models.Model): text = models.TextField(blank=True, verbose_name='Direction|text') forms.py class DirectionForm(forms.ModelForm):

Django post_save signal on parent class with multi-table inheritance

隐身守侯 提交于 2020-01-01 10:46:13
问题 In Django, if you have models that use multi-table inheritance, and you define a receiver for a post_save signal on the parent class, does that receiver function get called when an instance of the child class is saved? Borrowing an example from another question: class Animal(models.Model): category = models.CharField(max_length=20) class Dog(Animal): color = models.CharField(max_length=10) def echo_category(sender, **kwargs): print "category: '%s'" % kwargs['instance'].category post_save

How add a 'Star *' after a django ModelForm CharField?

◇◆丶佛笑我妖孽 提交于 2020-01-01 10:13:30
问题 i have some necessary fields in my django ModelForm. How can i add a red star (*) after the required fields ? 回答1: I'm going to assume you want this to happen automatically, so here's one of a few different ways: {% for field in form %} <label for="{{ field.auto_id }}">{{ field.label_tag }} {% if field.field.required %} <span class="required">*</span> {% endif %} </label> {% endfor %} Then you can style the asterisk using CSS. Or, you can add the asterisk using CSS instead if you want: <style

Undo or reset the faked migrations in django

巧了我就是萌 提交于 2020-01-01 10:09:46
问题 In my project which is based on django-1.8.2 I was facing some problems with migrations so i ran command python manage.py migrate --fake But it faked all the migrations which this command is meant for. But now python manage.py migrate command is not doing or applying any migration. I want to undo the faked migrations so that I can apply the migrations to database. I want to apply the existing migrations to the database. 回答1: For each app, you can fake the migrations back to where they were

Undo or reset the faked migrations in django

…衆ロ難τιáo~ 提交于 2020-01-01 10:08:14
问题 In my project which is based on django-1.8.2 I was facing some problems with migrations so i ran command python manage.py migrate --fake But it faked all the migrations which this command is meant for. But now python manage.py migrate command is not doing or applying any migration. I want to undo the faked migrations so that I can apply the migrations to database. I want to apply the existing migrations to the database. 回答1: For each app, you can fake the migrations back to where they were

'datetime.date' object has no attribute 'date'

二次信任 提交于 2020-01-01 09:42:15
问题 This code: import datetime d_tomorrow = datetime.date.today() + datetime.timedelta(days=1) class Model(models.Model): ... timeout = models.DateTimeField(null=True, blank=True, default=d_tomorrow) ... resuls in this error: 'datetime.date' object has no attribute 'date' What am I doing wrong? 回答1: d_tomorrow is expected, by the Django ORM, to have a date attribute (apparently), but doesn't. At any rate, you probably want to use a callable for the default date; otherwise, every model's default

Django avoids creating PointField in the database when I run python manage.py syncdb

人走茶凉 提交于 2020-01-01 09:38:17
问题 I'm using Django 1.2.3, PostGIS 1.5.2. For some reason when I run python manage.py syncdb it's creating all the other fields in the database from my models but avoids creating a field I named point that supposed to be a PointField. In my model.py files I have imported: from django.contrib.gis.db import models and commented: #from django.db import models my model looks something like this: class MyModel(models.Model): myid = models.AutoField(primary_key=True) title = models.CharField(max

Django db error: could not identify an equality operator for type json when trying to annotate a model with jsonfield

非 Y 不嫁゛ 提交于 2020-01-01 09:26:08
问题 I'm working in Django 1.5.4 and PostgreSQL 9.3, using django-jsonfield for JSONField. Following query throws db error (could not identify an equality operator for type json): ModelWithJsonField.objects.annotate(count=Count('field_to_count_by')) The field_to_count_by is not JSONField, normal int field. Any ideas how i can solve the issue and still use annotate? What annotate does behind the hood? 回答1: I ran into the same problem and finally (today) implemented a fake operator by running this

Django Signal via Decorator on Model Method?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-01 09:22:52
问题 I'm trying to do something like these proposed signal decorators. In addition to having a decorator that connects the decorated method to a signal (with the signal's sender as an argument to the decorator), I would like to use the decorator on class methods. I'd like to use the decorator like so: class ModelA(Model): @connect.post_save(ModelB) @classmethod def observe_model_b_saved(cls, sender, instance, created, **kwargs): # do some stuff pass The decorator is: from django.db.models import