Skip to content

Instantly share code, notes, and snippets.

View aliceridgway's full-sized avatar

Alice Ridgway aliceridgway

View GitHub Profile
@aliceridgway
aliceridgway / 0007_make_post_category_nonnullable.py
Created June 29, 2022 21:07
A custom migration that creates a category to be used as the default category for a post.
# Generated by Django 4.0.5 on 2022-06-29 20:33
from django.db import migrations
DEFAULT_CATEGORY = "Uncategorised"
def add_default_category(apps, _):
category_model = apps.get_model(app_label="blog", model_name="Category")
# Generated by Django 4.0.5 on 2022-06-04 18:08
import autoslug.fields
from django.db import migrations
from django.utils.text import slugify
DEFAULT = "abc"
def forwards(apps, _):
@aliceridgway
aliceridgway / test_demo.py
Created March 28, 2021 12:53
A simple Django test
from django.test import TestCase
def add_two_numbers(a, b):
return a + b
class TestExample(TestCase):
def test_add_two_numbers(self):
@aliceridgway
aliceridgway / urls.py
Last active June 25, 2022 17:31
Django: How to automatically log in users after registration
from django.urls import include, path
from . import views
urlpatterns = [
path('signup', views.SignUp.as_view(), name='signup'),
]
@aliceridgway
aliceridgway / forms.py
Last active June 25, 2022 17:19
How to pass the request object to a form class. Used when form choices are specific to a particular user
class CustomModelMultipleChoiceField(forms.ModelMultipleChoiceField):
def label_from_instance(self, member):
""" Customises the labels for checkboxes"""
return "%s" % member.name
class CreateMealForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
""" Grants access to the request object so that only members of the current user
are given as options"""
@aliceridgway
aliceridgway / forms.py
Created September 27, 2020 09:43
How to use the user object in Django forms. Used when choices in a multi-select-field are unique to a given user.
from django import forms
from .models import Meal
from core.models import Member
class CustomModelMultipleChoiceField(forms.ModelMultipleChoiceField):
def label_from_instance(self, member):
return "%s" % member.name
@aliceridgway
aliceridgway / models.py
Created June 11, 2022 15:19
Movie Model
from django.db import models
from autoslug import AutoSlugField
from django.urls import reverse
class Genre(models.Model):
name = models.CharField(max_length=100)
api_id = models.IntegerField(unique=True)
@aliceridgway
aliceridgway / tests.py
Created June 11, 2022 15:14
Testing Django Models Example
from datetime import datetime
from django.db import IntegrityError
from django.test import TestCase
from django.urls import reverse
from django.utils.text import slugify
from .models import Genre, Movie
from django.db import models
from django.contrib import auth
USER = auth.get_user_model()
class UserProfile(models.Model):
user = models.OneToOneField(to=USER, related_name="profile", on_delete=models.PROTECT)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
bio = models.TextField()
from datetime import datetime
from django.db import IntegrityError
from django.test import TestCase
from django.urls import reverse
from django.utils.text import slugify
from .models import Genre, Movie