Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CRUD operations to items and categories for admin users (#3)
- Loading branch information
1 parent
466d330
commit fe4d92b
Showing
7 changed files
with
174 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,6 +69,8 @@ | |
}, | ||
] | ||
|
||
LIST_PER_PAGE = int(env("LIST_PER_PAGE")) | ||
|
||
WSGI_APPLICATION = 'config.wsgi.application' | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from django.conf import settings | ||
from django.contrib import admin | ||
|
||
from .forms import ItemAdminForm | ||
from .models import Item, Category | ||
|
||
|
||
@admin.register(Item) | ||
class ItemAdmin(admin.ModelAdmin): | ||
list_per_page = settings.LIST_PER_PAGE | ||
form = ItemAdminForm | ||
search_fields = ('categories__title',) | ||
|
||
|
||
@admin.register(Category) | ||
class CategoryAdmin(admin.ModelAdmin): | ||
list_per_page = settings.LIST_PER_PAGE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from django import forms | ||
from django.utils.translation import ugettext_lazy as _ | ||
from django.contrib.admin.widgets import FilteredSelectMultiple | ||
from .models import Item, Category | ||
|
||
|
||
class ItemAdminForm(forms.ModelForm): | ||
categories = forms.ModelMultipleChoiceField( | ||
queryset=Category.objects.all(), | ||
required=False, | ||
widget=FilteredSelectMultiple( | ||
verbose_name=_("Categories"), | ||
is_stacked=False | ||
) | ||
) | ||
|
||
class Meta: | ||
model = Item | ||
fields = '__all__' | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(ItemAdminForm, self).__init__(*args, **kwargs) | ||
|
||
if self.instance.id: | ||
self.fields["categories"].initial = self.instance.categories.all() | ||
|
||
def save(self, commit=True): | ||
article = super(ItemAdminForm, self).save(commit=False) | ||
if self.cleaned_data["categories"]: | ||
article.save() | ||
article.categories.set(self.cleaned_data["categories"]) | ||
if commit: | ||
article.save() | ||
article.save_m2m() | ||
|
||
return article |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Generated by Django 3.1 on 2020-08-12 07:08 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Item', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('title', models.CharField(max_length=200, verbose_name='Title')), | ||
('description', models.TextField(verbose_name='Description')), | ||
('link', models.URLField(verbose_name='Link')), | ||
('comments', models.URLField(verbose_name='Comments Link')), | ||
('pub_date', models.DateTimeField(blank=True, null=True, verbose_name='Published Date')), | ||
('guid', models.IntegerField(default=0, verbose_name='GUID')), | ||
], | ||
options={ | ||
'verbose_name_plural': 'Items', | ||
'db_table': 'feeds_items', | ||
}, | ||
), | ||
migrations.CreateModel( | ||
name='Category', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('title', models.CharField(max_length=50, unique=True, verbose_name='Category')), | ||
('items', models.ManyToManyField(related_name='categories', to='feeds.Item')), | ||
], | ||
options={ | ||
'verbose_name_plural': 'Categories', | ||
'db_table': 'feeds_categories', | ||
}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from django.db import models | ||
|
||
|
||
class Item(models.Model): | ||
title = models.CharField(verbose_name="Title", max_length=200) | ||
description = models.TextField(verbose_name="Description") | ||
link = models.URLField(verbose_name="Link") | ||
comments = models.URLField(verbose_name="Comments Link") | ||
pub_date = models.DateTimeField( | ||
verbose_name="Published Date", null=True, blank=True) | ||
guid = models.IntegerField(verbose_name="GUID", default=0) | ||
|
||
class Meta: | ||
db_table = "feeds_items" | ||
verbose_name_plural = "Items" | ||
|
||
def __str__(self): | ||
return f"{self.title}" | ||
|
||
|
||
class Category(models.Model): | ||
title = models.CharField(verbose_name="Category", | ||
max_length=50, unique=True) | ||
items = models.ManyToManyField(Item, related_name="categories") | ||
|
||
class Meta: | ||
db_table = "feeds_categories" | ||
verbose_name_plural = "Categories" | ||
|
||
def __str__(self): | ||
return f"{self.title}" |