1

I'm trying to implement the CRUD function using GenericView + ClassBaseView, but I'm stumped on the ListView display part.
If anyone has a solution, please let me know.

I'm not very experienced with django and web apps, and I'm not familiar with the English language, so I apologize if I'm misrepresenting things.
(Note that we have already implemented the CRUD functionality (without django-betterforms) in ClassBaseView for a single table only.)

■Remarks

  • There are no foreign keys between all tables.
  • Duplicate column names and variable names in some related tables.
    (→variable names become the same when you do an inspectdb).
  • I tried looking at the official site and stackoverflow's related questions (questions/569468),
    but it didn't work because I didn't know how to write it properly.

■Hope (constraints)

  • I don't want to make any changes to the existing DB side as much as possible for the DB in production.

  • For the sake of readability, we want to implement it as much as possible using a combination of ORM and ClassBaseView.

  • Due to the (old) style of development,
    where DB table definitions are directly changed,
    I would like to only use models.py as a result of inspectdb.

  • We want to only use models.py that is the result of inspectdb as much as possible, but However,
    if we have to change models.py, we want to minimize the amount of change.

models.py

class employee(models.Model):
    employeecd = models.IntegerField(db_column='employeeCD', primary_key=True)  # Field name made lowercase.
    belongscd = models.IntegerField(db_column='belongsCD', blank=True, null=True)  # Field name made lowercase.
    positioncd = models.IntegerField(db_column='positionCD', blank=True, null=True)  # Field name made lowercase.
    name = models.CharField(max_length=255, blank=True, null=True)

    class Meta:
        db_table = 'employee'


class position(models.Model):
    positioncd = models.IntegerField(db_column='positionCD', primary_key=True)  # Field name made lowercase.
    position = models.CharField(max_length=50, blank=True, null=True)

    class Meta:
        db_table = 'position'


class belongs(models.Model):
    belongscd = models.IntegerField(db_column='belongsCD', primary_key=True)  # Field name made lowercase.
    belongs = models.CharField(max_length=50, blank=True, null=True)

    class Meta:
        db_table = 'belongs'

forms.py

from betterforms.multiform import MultiForm


class employeeMultiForm(forms.ModelForm):
    class Meta:
        model = employee
        fields = ['employeecd', 'name', 'positioncd', 'belongscd']


class positionMultiForm(forms.ModelForm):
    class Meta:
        model = position
        fields = ['positioncd', 'position']


class belongsMultiForm(forms.ModelForm):
    class Meta:
        model = belongs
        fields = ['belongscd', 'belongs']


class UserProfileMultiForm(MultiForm):
    form_classes = {
        'employee': employeeMultiForm,
        'position': positionMultiForm,
        'belongs': belongsMultiForm,
    }

# Do I need to init UserProfileMultiForm?
# Why not get_context_data() or get_queryset()?
# I don't know how to write the initial data. If I write it the way below
# → AttributeError: type object 'employeeMultiForm' has no attribute 'object'
# UserProfileMultiForm(initial={
#     'employee': {
#         # User's initial data
#         employeeMultiForm.object.all()
#     },
#     'position': {
#         # Profile's initial data
#         positionMultiForm.object.all()
#     },
#     'belongs': {
#         # Profile's initial data
#         belongsMultiForm.object.all()
#     },
# })

view.py(The behavior of this file has not been verified because of an error in forms.py.)

class usingmultiformemployeeListView(ListView):

    model = employee  # If you comment out →ImproperlyConfigured at /app_name/xxxx/
    form_class = UserProfileMultiForm
    template_name = 'xxxx.html'
    ordering = ['employeecd']

    def get_context_data(self, **kwargs):
        context = super(usingmultiformemployeeListView, self).get_context_data(**kwargs)
        context.update({
        'form': UserProfileMultiForm()
        })
        return context

    def get_queryset(self):
        queryset = UserProfileMultiForm['employee'].fields['employeecd', 'name']
        return queryset

xxxx.html (The behavior of this file has not been verified because of an error in forms.py.)

    {% for o in object_list %}
          employeecd:{{ o.employee.employeecd }}
          name:{{ o.employee.name }}
          position:{{ o.position.position }}
          belongs:{{ o.belongs.belongs }}
    {% endfor %}

I'll add a note about what I want to do:
It is a mistranslation to say that it implements the CRUD function;
the correct translation is that it implements the CRUD features.

This question is only for the Read feature.
What I want to do is get the same result as the following SQL and display it in table format.

SELECT employee.employeecd
      ,employee.name
      ,employee.belongscd
      ,belongs.belongs
      ,employee.positioncd
      ,position.position
     
FROM employee
INNER JOIN belongs on employee.belongscd = belongs.belongscd
INNER JOIN position on employee.positioncd = position.positioncd
Worried
  • 31
  • 3

0 Answers0