0

Are there better approaches to this problem? This model structure is what I am after for my database. So I am feeling like I am breaking the DRY rule with my learning project.

Using Django 3.2.9 & Python 3.9.6

I have looked around the internet on how to get this more dynamic in the views, urls & templates but I have not had any luck.

I have the following model structure for my models/contact.py

class BaseContact(AbstractUser):
    other_name = models.CharField(max_length=50, null=True, blank=True)
    contact_number = models.CharField(max_length=10, null=True, blank=True)
    email = models.EmailField(unique=True)
    postal_address = models.ForeignKey(location.Address, on_delete=models.CASCADE, null=True, blank=True)
    tax_id = models.CharField(max_length=11, null=True, blank=True)
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['first_name']

    class Meta:
        default_related_name = 'base'
        verbose_name = 'Contact'
        verbose_name_plural = 'Contacts'

    def get_absolute_url(self):
        return reverse("%s:detail-%s" % (str(type(self).__name__.lower()), str(type(self).__name__.lower())),
                       kwargs={"pk": self.pk})


class Customer(BaseContact):

    class Meta:
        default_related_name = 'customer'


class Supplier(BaseContact):
    class Meta:
        default_related_name = 'supplier'


class Employee(BaseContact):
    class Meta:
        default_related_name = 'employee'
        verbose_name = 'Employee'
        verbose_name_plural = 'Employees'

So my urls/contact.py is looking grim.

from django.urls import path
from main.views.contact import UpdateEmployeeView, DetailEmployeeView, CreateEmployeeView, DeleteEmployeeView
from main.views.contact import UpdateSupplierView, DetailSupplierView, CreateSupplierView, DeleteSupplierView
from main.views.contact import UpdateCustomerView, DetailCustomerView, CreateCustomerView, DeleteCustomerView

urlpatterns = [
    path('employee/create/', CreateEmployeeView.as_view(), name='create-employee'),
    path('employee/detail/<int:pk>', DetailEmployeeView.as_view(), name='detail-employee'),
    path('employee/update/<int:pk>', UpdateEmployeeView.as_view(), name='update-employee'),
    path('employee/delete/<int:pk>', DeleteEmployeeView.as_view(), name='delete-employee'),

    path('supplier/create/', CreateSupplierView.as_view(), name='create-supplier'),
    path('supplier/detail/<int:pk>', DetailSupplierView.as_view(), name='detail-supplier'),
    path('supplier/update/<int:pk>', UpdateSupplierView.as_view(), name='update-supplier'),
    path('supplier/delete/<int:pk>', DeleteSupplierView.as_view(), name='delete-supplier'),

    path('customer/create/', CreateCustomerView.as_view(), name='create-customer'),
    path('customer/detail/<int:pk>', DetailCustomerView.as_view(), name='detail-customer'),
    path('customer/update/<int:pk>', UpdateCustomerView.as_view(), name='update-customer'),
    path('customer/delete/<int:pk>', DeleteCustomerView.as_view(), name='delete-customer'),
]

not much better in the views/contact.py

class CreateEmployeeView(CreateView):
    model = Employee
    template_name_suffix = '_create_form'
    fields = ['__all__']


class DetailEmployeeView(DetailView):
    model = Employee


class UpdateEmployeeView(UpdateView):
    model = Employee
    form_class = UpdateEmployeeForm
    template_name_suffix = '_update_form'


class DeleteEmployeeView(DeleteView):
    model = Employee
    success_url = reverse_lazy('home')


class CreateSupplierView(CreateView):
    model = Supplier
    template_name_suffix = '_create_form'
    fields = ['__all__']


class DetailSupplierView(DetailView):
    model = Supplier


class UpdateSupplierView(UpdateView):
    model = Supplier
    form_class = UpdateSupplierForm
    template_name_suffix = '_update_form'


class DeleteSupplierView(DeleteView):
    model = Supplier
    success_url = reverse_lazy('home')


class CreateCustomerView(CreateView):
    model = Customer
    template_name_suffix = '_create_form'
    fields = ['__all__']


class DetailCustomerView(DetailView):
    model = Customer


class UpdateCustomerView(UpdateView):
    model = Customer
    form_class = UpdateCustomerForm
    template_name_suffix = '_update_form'


class DeleteCustomerView(DeleteView):
    model = Customer
    success_url = reverse_lazy('home')

templates/main folder

Rishu
  • 1
  • 2
  • Why doesn't this look DRY to you? If your app requries so many views, then defining all the views (and their respective paths) doesn't seem to violate DRY principles. – yagus Nov 26 '21 at 11:18
  • Hmm. I thought there may have been a less DRY way to approach this. I was reading the [namespace](https://docs.djangoproject.com/en/3.2/topics/http/urls/#reversing-namespaced-urls) documentation, I read it as I could have one set of views (Create, Update, Delete, Detail) and the urls is the code that changed for each different scenario of user. If the path im taking seems right, then thats cool. thanks! – Rishu Nov 26 '21 at 12:10
  • Your views do different things, using different forms. So it's correct to use different views. The namespacing you're referring to is usually used to identify path names in different apps, even though the names are identical (if you have multiple apps with the same path name, there wouldn't be any conflict with namespacing). – yagus Nov 26 '21 at 12:17
  • Awesome! Still new to it all, self learning. Nice knowing I'm on the right path. I become worried when I start repeating code is all I guess. Cheers for the help – Rishu Nov 26 '21 at 12:27

0 Answers0