How to query as GROUP BY in django?

If you mean to do aggregation you can use the aggregation features of the ORM: from django.db.models import Count result = (Members.objects .values(‘designation’) .annotate(dcount=Count(‘designation’)) .order_by() ) This results in a query similar to SELECT designation, COUNT(designation) AS dcount FROM members GROUP BY designation and the output would be of the form [{‘designation’: ‘Salesman’, ‘dcount’: 2}, … Read more

How can I get the full/absolute URL (with domain) in Django?

Use handy request.build_absolute_uri() method on request, pass it the relative url and it’ll give you full one. By default, the absolute URL for request.get_full_path() is returned, but you can pass it a relative URL as the first argument to convert it to an absolute URL. >>> request.build_absolute_uri() ‘https://example.com/music/bands/the_beatles/?print=true’ >>> request.build_absolute_uri(‘/bands/?print=true’) ‘https://example.com/bands/?print=true’

RuntimeWarning: DateTimeField received a naive datetime

The problem is not in Django settings, but in the date passed to the model. Here’s how a timezone-aware object looks like: >>> from django.utils import timezone >>> import pytz >>> timezone.now() datetime.datetime(2013, 11, 20, 20, 8, 7, 127325, tzinfo=pytz.UTC) And here’s a naive object: >>> from datetime import datetime >>> datetime.now() datetime.datetime(2013, 11, 20, … Read more

In a Django form, how do I make a field readonly (or disabled) so that it cannot be edited?

As pointed out in this answer, Django 1.9 added the Field.disabled attribute: The disabled boolean argument, when set to True, disables a form field using the disabled HTML attribute so that it won’t be editable by users. Even if a user tampers with the field’s value submitted to the server, it will be ignored in … Read more

No module named MySQLdb

You need to use one of the following commands. Which one depends on what OS and software you have and use. easy_install mysql-python (mix os) pip install mysql-python (mix os/ python 2) pip install mysqlclient (mix os/ python 3) apt-get install python-mysqldb (Linux Ubuntu, …) cd /usr/ports/databases/py-MySQLdb && make install clean (FreeBSD) yum install MySQL-python … Read more

‘pip’ is not recognized as an internal or external command

You need to add the path of your pip installation to your PATH system variable. By default, pip is installed to C:\Python34\Scripts\pip (pip now comes bundled with new versions of python), so the path “C:\Python34\Scripts” needs to be added to your PATH variable. To check if it is already in your PATH variable, type echo … Read more

Extending the User model with custom fields in Django

The least painful and indeed Django-recommended way of doing this is through a OneToOneField(User) property. Extending the existing User model … If you wish to store information related to User, you can use a one-to-one relationship to a model containing the fields for additional information. This one-to-one model is often called a profile model, as … Read more

No module named pkg_resources

July 2018 Update Most people should now use pip install setuptools (possibly with sudo). Some may need to (re)install the python-setuptools package via their package manager (apt-get install, yum install, etc.). This issue can be highly dependent on your OS and dev environment. See the legacy/other answers below if the above isn’t working for you. … Read more

How to define two fields “unique” as couple

There is a simple solution for you called unique_together which does exactly what you want. For example: class MyModel(models.Model): field1 = models.CharField(max_length=50) field2 = models.CharField(max_length=50) class Meta: unique_together = (‘field1’, ‘field2′,) And in your case: class Volume(models.Model): id = models.AutoField(primary_key=True) journal_id = models.ForeignKey(Journals, db_column=’jid’, null=True, verbose_name = “Journal”) volume_number = models.CharField(‘Volume Number’, max_length=100) comments = … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)