T
T
theoctopus2016-01-20 16:30:39
Django
theoctopus, 2016-01-20 16:30:39

How to give each field a separate class?

Hello everyone,
I have a form to add vacancies. I need to understand how can I make each field have its own design? For example, if this is a job title, then it should have its own div class, and the other field should have a different one.
I'm still inexperienced and learning, so do not judge strictly.
When rendering form.as_p it doesn't output as expected.
models.py

class Vacancies (models.Model):
    vac_name = models.CharField("Название вакансии", max_length=50)
    vac_req = models.TextField("Требования", max_length=1000)
    vac_obz = models.TextField("Обязанности", max_length=1000)
    vac_usl = models.TextField("Условия",max_length=1000)
    vac_salary = models.IntegerField("Зарплата", blank=False, default=0)
    vac_comp = models.ForeignKey(User,
                               related_name='company_name')
    vac_zan = models.ForeignKey('WorkTime', verbose_name="Выберите тип занятости")
    vac_locat = models.ForeignKey('LocatOn', verbose_name="Город")
    vac_exp = models.ForeignKey('Expierence', verbose_name="Требуемый опыт")
    vac_cat = models.ForeignKey('VacanCategory', verbose_name="Выберите категорию")
    created_date = models.DateTimeField(auto_now_add=True)
    modified_date = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.vac_name


class CompanyName (models.Model):
    comp_name = models.CharField("Название компании", max_length=50, default=" ##Название компании## ")

    def __str__(self):
        return self.comp_name


class WorkTime (models.Model):
    work_t = models.CharField("Время работы", max_length=30, default=" ##Режим Работы## ")

    def __str__(self):
        return self.work_t


class LocatOn (models.Model):
    location_city = models.CharField("Город", max_length=30, default=" ##Название города## ")

    def __str__(self):
        return self.location_city


class Expierence (models.Model):
    exp_srok = models.CharField ("Опыт работы", max_length=10, default=" ##ОПЫТ РАБОТЫ## ")

    def __str__(self):
        return self.exp_srok

class VacanCategory (models.Model):
    vacan_cat = models.CharField ("Категория вакансии", max_length=45, default=" ##ОСНОВНАЯ КАТЕГОРИЯ## ")

    def __str__(self):
        return self.vacan_cat

forms.py
from django import forms
from .models import Vacancies

class VacForm(forms.ModelForm):
    class Meta:
        model = Vacancies
        fields = ('vac_name', 'vac_req','vac_obz', 'vac_usl','vac_salary', 'vac_comp','vac_zan', 'vac_locat', 'vac_exp','vac_cat', )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2016-01-20
@theoctopus

I'm still inexperienced and learning, so do not judge strictly.

In my opinion, just when you are inexperienced, you need to judge as strictly as possible in order to straighten your hands in the right direction as quickly as possible. And if you get upset from criticism and quit it all - well, okay, less competition.
I have already written this a hundred times here and will write more until this tin is.
Here you have created a model of vacancies. You model describe single essence, and called Vacancies - plural.
Further: here you have a model of vacancies. Why do you give each field a name that duplicates the name of the model? (I'm talking about vac_*). You created a vacancy, yes:
And went to get attributes:
Abbreviated names of attributes - beauty. In six months, you will have no idea what vac_zan, vac_usl, vac_cat are. And the person who will work with you in a team or refactor it after you will receive an excellent set of puzzles as a gift to the code.
Simply chic intricacies of Russian and English. Your code reflects your versatility and uniqueness. You are a polyglot. Is that what you think when you make this nonsense?
Unfortunately, right now I am busy refactoring such code and for the first two days I cried over this code and refused to sit down for it. The customer persuaded me on the condition that I completely rewrite the same tin.
Question answered: https://pypi.python.org/pypi/django-widget-tweaks

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question