What do you want to save?
Add Code snippet
New code examples
-
Python 2021-11-06 01:06:11
django unique slug
from django.template.defaultfilters import slugify # in the view maybe while saving the form... def savedata() form = someform(request.POST or None) if form.is_valid(): new_post = form.save(commit=False) #for holding it before actually sav... Add solution -
Python 2021-10-28 18:44:13
using regex validators in django models
from django.core.validators import RegexValidator from django.db import models PHONE_NUMBER_REGEX = RegexValidator(r'^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*$', 'only valid email is required') class SomeClass(models.Model): phone = models.CharField(... Add solution -
Python 2021-10-11 18:00:15
how to get parent model object based on child model filter in django
#Model class Kid(models.Model): name = models.CharField(max_length=200) class Toy(models.Model): name = models.CharField(max_length=200) owner = models.ForeignKey(Kid) #view Kid.objects.distinct().filter(toy__name__icontains='star') Add solution -
Python 2021-10-09 14:43:03
django admin slug auto populate
#models.py from django.db import models from django_extensions.db.fields import AutoSlugField class MyModel(models.Model): title = models.CharField(max_length=42) slug = AutoSlugField(populate_from=['title', 'description', 'get_author_name']) Add solution -
Python 2021-10-04 15:16:05
pre filled from django
# ======================================================= # Non-Model Forms - use 'initial' and supply a dictionary # views.py form = YourForm(initial={'charfield1': 'foo', 'charfield2': 'bar'}) # forms.py class YourForm(forms.Form): charfield1 = form... Add solution -
Python 2021-10-03 08:40:02
super in django manager
# First, define the Manager subclass. class DahlBookManager(models.Manager): def get_queryset(self): return super().get_queryset().filter(author='Roald Dahl') # Then hook it into the Book model explicitly. class Book(models.Model): title ... Add solution
Best helpers
+5
+5
+5
+5
+5
+5
+5
+5
+5
+5
+5
+5
+5
+5