class Category(models.Model):
name = models.CharField(max_length=255)
class Post(models.Model):
title = models.CharField(max_length=255)
description = models.TextField()
post = models.TextField()
pub_date = models.DateField()
categories = models.ManyToManyField(Category, blank=True)
BEGIN;
CREATE TABLE `posts_category` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`name` varchar(255) NOT NULL
)
;
CREATE TABLE `posts_post` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`title` varchar(255) NOT NULL,
`description` longtext NOT NULL,
`post` longtext NOT NULL,
`pub_date` date NOT NULL
)
;
CREATE TABLE `posts_post_categories` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`post_id` integer NOT NULL,
`category_id` integer NOT NULL,
UNIQUE (`post_id`, `category_id`)
)
;
ALTER TABLE `posts_post_categories` ADD CONSTRAINT `post_id_refs_id_2bd0ebb3` FOREIGN KEY (`post_id`) REFERENCES `posts_post` (`id`);
ALTER TABLE `posts_post_categories` ADD CONSTRAINT `category_id_refs_id_65af7dcb` FOREIGN KEY (`category_id`) REFERENCES `posts_category` (`id`);
COMMIT;
UNIQUE (`post_id`, `category_id`)
Спасибо.