python
52 lines · 1 tab
Priya Sharma
Jan 2026
1 tab
import factory
from factory.django import DjangoModelFactory
from factory import Faker, SubFactory, post_generation
from blog.models import Post, Comment, Tag
from django.contrib.auth import get_user_model
User = get_user_model()
class UserFactory(DjangoModelFactory):
class Meta:
model = User
email = Faker('email')
first_name = Faker('first_name')
last_name = Faker('last_name')
is_active = True
class TagFactory(DjangoModelFactory):
class Meta:
model = Tag
name = factory.Sequence(lambda n: f'tag-{n}')
class PostFactory(DjangoModelFactory):
class Meta:
model = Post
title = Faker('sentence', nb_words=6)
content = Faker('paragraph', nb_sentences=10)
author = SubFactory(UserFactory)
status = 'published'
@post_generation
def tags(self, create, extracted, **kwargs):
if not create:
return
if extracted:
for tag in extracted:
self.tags.add(tag)
class CommentFactory(DjangoModelFactory):
class Meta:
model = Comment
post = SubFactory(PostFactory)
author = SubFactory(UserFactory)
content = Faker('paragraph')
approved = True
1 file · python
Explain with highlit
Factory Boy eliminates boilerplate in tests by generating model instances with sensible defaults. I use Sequence for unique values, SubFactory for foreign keys, and post_generation for many-to-many relationships. The Faker integration provides realistic data. I create factories in tests/factories.py and import them across test files. For related objects, LazyAttribute computes values based on other fields. This makes tests more readable and maintainable than manual object creation. I can override any attribute when calling the factory.
Related snips
ruby
class CommentsController < ApplicationController
before_action :set_post
def create
@comment = @post.comments.build(comment_params)
System test: asserting Turbo Stream responses
rails
hotwire
turbo
by codesnips
4 tabs
python
import os
import stat
for root, _dirs, files in os.walk('/etc'):
for name in files:
path = os.path.join(root, name)
Python security audit script for exposed risky filesystem state
python
auditing
host-security
by Kai Nakamura
1 tab
python
class Product(models.Model):
name = models.CharField(max_length=200)
slug = models.SlugField(blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
cost = models.DecimalField(max_digits=10, decimal_places=2)
margin = models.DecimalField(max_digits=5, decimal_places=2, blank=True)
Django model signals vs overriding save
django
python
models
by Priya Sharma
2 tabs
python
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns = [
Django URL namespacing and reverse lookups
django
python
urls
by Priya Sharma
3 tabs
python
import graphene
from graphene_django import DjangoObjectType
from blog.models import Post, Comment
class PostType(DjangoObjectType):
Django GraphQL with Graphene
django
python
graphql
by Priya Sharma
2 tabs
rust
use my_crate::add;
#[test]
fn test_public_api() {
assert_eq!(add(3, 4), 7);
}
Integration tests in tests/ directory
rust
testing
integration
by Marcus Chen
1 tab
Share this code
Here's the card — post it anywhere.