Django custom managers – how do I return only objects created by the logged-in user?

One way to handle this would be to create a new method instead of redefining get_query_set. Something along the lines of: class UserContactManager(models.Manager): def for_user(self, user): return super(UserContactManager, self).get_query_set().filter(creator=user) class UserContact(models.Model): […] objects = UserContactManager() This allows your view to look like this: contacts = Contact.objects.for_user(request.user) This should help keep your view simple, and because … Read more

Django: show a ManyToManyField in a template?

Use place.area.all in the template http://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships {% for place in places %} Name: {{ place.name }}<br/> Area: <br/>{% for area in place.area.all %}{{ area }}<br/>{% endfor %} {% endfor %}

Django: signal when user logs in?

You can use a signal like this (I put mine in models.py) from django.contrib.auth.signals import user_logged_in def do_stuff(sender, user, request, **kwargs): whatever… user_logged_in.connect(do_stuff) See django docs: https://docs.djangoproject.com/en/dev/ref/contrib/auth/#module-django.contrib.auth.signals and here http://docs.djangoproject.com/en/dev/topics/signals/

get request data in Django form

As ars and Diarmuid have pointed out, you can pass request.user into your form, and use it in validating the email. Diarmuid’s code, however, is wrong. The code should actually read: from django import forms class UserForm(forms.Form): email_address = forms.EmailField( widget=forms.TextInput( attrs={ ‘class’: ‘required’ } ) ) def __init__(self, *args, **kwargs): self.user = kwargs.pop(‘user’, None) … Read more

How to show a PDF file in a Django view?

Django has a class specifically for returning files, FileResponse. It streams files, so that you don’t have to read the entire file into memory before returning it. Here you go: from django.http import FileResponse, Http404 def pdf_view(request): try: return FileResponse(open(‘foobar.pdf’, ‘rb’), content_type=”application/pdf”) except FileNotFoundError: raise Http404() If you have really large files or if you’re … Read more

Enforce unique upload file names using django?

Use uuid. To tie that into your model see Django documentation for FileField upload_to. For example in your models.py define the following function: import uuid import os def get_file_path(instance, filename): ext = filename.split(‘.’)[-1] filename = “%s.%s” % (uuid.uuid4(), ext) return os.path.join(‘uploads/logos’, filename) Then, when defining your FileField/ImageField, specify get_file_path as the upload_to value. file = … Read more

Is it possible to generate django models from the database?

Yes, use the inspectdb command: http://docs.djangoproject.com/en/dev/ref/django-admin/#inspectdb inspectdb Introspects the database tables in the database pointed-to by the DATABASE_NAME setting and outputs a Django model module (a models.py file) to standard output. Use this if you have a legacy database with which you’d like to use Django. The script will inspect the database and create a … Read more

how to embed standalone bokeh graphs into django templates

Using the Embedding Bokeh Plots documentation example as suggested by Fabio Pliger, one can do this in Django: in the views.py file, we put: from django.shortcuts import render from bokeh.plotting import figure from bokeh.resources import CDN from bokeh.embed import components def simple_chart(request): plot = figure() plot.circle([1,2], [3,4]) script, div = components(plot, CDN) return render(request, “simple_chart.html”, … Read more

Django Multiple Choice Field / Checkbox Select Multiple

The profile choices need to be setup as a ManyToManyField for this to work correctly. So… your model should be like this: class Choices(models.Model): description = models.CharField(max_length=300) class Profile(models.Model): user = models.ForeignKey(User, blank=True, unique=True, verbose_name=”user”) choices = models.ManyToManyField(Choices) Then, sync the database and load up Choices with the various options you want available. Now, the … Read more