diff --git a/example/blog/admin.py b/example/blog/admin.py index c7a193c..c30e33e 100644 --- a/example/blog/admin.py +++ b/example/blog/admin.py @@ -1,5 +1,14 @@ from django.contrib import admin -from blog.models import Article +from blog.models import Article, Comment -admin.site.register(Article) + +class CommentAdminInline(admin.StackedInline): + model = Comment + + +class ArticleAdmin(admin.ModelAdmin): + inlines = [CommentAdminInline] + + +admin.site.register(Article, ArticleAdmin) diff --git a/example/blog/migrations/0005_alter_article_excerpt_comment.py b/example/blog/migrations/0005_alter_article_excerpt_comment.py new file mode 100644 index 0000000..986e200 --- /dev/null +++ b/example/blog/migrations/0005_alter_article_excerpt_comment.py @@ -0,0 +1,50 @@ +# Generated by Django 4.2.14 on 2024-07-17 08:14 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion +import prose.fields + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ("blog", "0004_alter_article_excerpt"), + ] + + operations = [ + migrations.AlterField( + model_name="article", + name="excerpt", + field=prose.fields.RichTextField(blank=True, default=""), + ), + migrations.CreateModel( + name="Comment", + fields=[ + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("body", prose.fields.RichTextField(blank=True, default="")), + ( + "article", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, to="blog.article" + ), + ), + ( + "author", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to=settings.AUTH_USER_MODEL, + ), + ), + ], + ), + ] diff --git a/example/blog/models.py b/example/blog/models.py index 8fdbf7d..1e5b2df 100644 --- a/example/blog/models.py +++ b/example/blog/models.py @@ -11,8 +11,17 @@ class Article(models.Model): settings.AUTH_USER_MODEL, on_delete=models.CASCADE, ) - excerpt = RichTextField(blank=True, null=True) + excerpt = RichTextField(blank=True, default="") body = models.OneToOneField(Document, on_delete=models.CASCADE) def __str__(self): return f"{self.title} by {self.author.username}: {self.body}" + + +class Comment(models.Model): + article = models.ForeignKey(Article, on_delete=models.CASCADE) + author = models.ForeignKey( + settings.AUTH_USER_MODEL, + on_delete=models.CASCADE, + ) + body = RichTextField(blank=True, default="") diff --git a/prose/static/prose/editor.js b/prose/static/prose/editor.js index 4f9b2df..91d3d47 100644 --- a/prose/static/prose/editor.js +++ b/prose/static/prose/editor.js @@ -57,8 +57,25 @@ function initializeEditors() { }); } +/** + * https://github.com/withlogicco/django-prose/issues/100 + */ +function patchTrixEditorWithNameSetter() { + Object.defineProperty(window.Trix.elements.TrixEditorElement.prototype, "name", { + get() { + return this.inputElement?.name; + }, + set(value) { + this.inputElement.name = value; + }, + }); +} + // When the DOM is initially loaded -document.addEventListener("DOMContentLoaded", initializeEditors); +document.addEventListener("DOMContentLoaded", () => { + initializeEditors(); + patchTrixEditorWithNameSetter(); +}); // Export the initializeEditors function so it can be called from other scripts window.djangoProse = window.djangoProse || {};