What’s the difference between django OneToOneField and ForeignKey?

Differences between OneToOneField(SomeModel) and ForeignKey(SomeModel, unique=True) as stated in The Definitive Guide to Django: OneToOneField A one-to-one relationship. Conceptually, this is similar to a ForeignKey with unique=True, but the “reverse” side of the relation will directly return a single object. In contrast to the OneToOneField “reverse” relation, a ForeignKey “reverse” relation returns a QuerySet. Example … Read more

Creating a JSON response using Django and Python

I usually use a dictionary, not a list to return JSON content. import json from django.http import HttpResponse response_data = {} response_data[‘result’] = ‘error’ response_data[‘message’] = ‘Some error message’ Pre-Django 1.7 you’d return it like this: return HttpResponse(json.dumps(response_data), content_type=”application/json”) For Django 1.7+, use JsonResponse as shown in this SO answer like so : from django.http … Read more

How to filter empty or NULL names in a QuerySet?

You could do this: Name.objects.exclude(alias__isnull=True) If you need to exclude null values and empty strings, the preferred way to do so is to chain together the conditions like so: Name.objects.exclude(alias__isnull=True).exclude(alias__exact=””) Chaining these methods together basically checks each condition independently: in the above example, we exclude rows where alias is either null or an empty string, … Read more

Capturing URL parameters in request.GET

When a URL is like domain/search/?q=haha, you would use request.GET.get(‘q’, ”). q is the parameter you want, and ” is the default value if q isn’t found. However, if you are instead just configuring your URLconf**, then your captures from the regex are passed to the function as arguments (or named arguments). Such as: (r’^user/(?P<username>\w{0,50})/$’, … Read more

Separation of business logic and data access in django

It seems like you are asking about the difference between the data model and the domain model – the latter is where you can find the business logic and entities as perceived by your end user, the former is where you actually store your data. Furthermore, I’ve interpreted the 3rd part of your question as: how … Read more

What is related_name used for?

The related_name attribute specifies the name of the reverse relation from the User model back to your model. If you don’t specify a related_name, Django automatically creates one using the name of your model with the suffix _set, for instance User.map_set.all(). If you do specify, e.g. related_name=maps on the User model, User.map_set will still work, … Read more

How can I upgrade specific packages using pip and a requirements file?

I ran the following command and it upgraded from 1.2.3 to 1.4.0 pip install Django –upgrade Shortcut for upgrade: pip install Django -U Note: if the package you are upgrading has any requirements this command will additionally upgrade all the requirements to the latest versions available. In recent versions of pip, you can prevent this … Read more

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