写一个视图,允许用户添加相关数据的客户,例如像意见; 在单页上(Writing a view t

2019-10-29 15:06发布

我有一个Django CRUD应用程序管理功能,客户对业务工作模式,表单,视图和网址。 我只是不能似乎找出如何写一个视图,以允许用户添加注释,或与客户使用的单一视图和模板存储在其他型号的其他数据。

因此,例如, 对于一个客户,一切为了客户一个与该选项的意见补充,修改等,并同为其他相关模型。

我知道如何做到这一点对一个我就能尽快取得进展。 (这里的老同学程序员)

以下是我与工作 - 保持简单。

楷模

class Emergency(models.Model):

    # Fields
    name = CharField(null = False, blank = False, max_length=60)
    address = TextField(blank=True, null=True, help_text='Street and town', verbose_name='Address')
    telephone = CharField(blank=False, null=False, unique= True, max_length=20)
    relationship = CharField(choices=(('P',  'Parent'),('S',  'Son'),('D',  'Daughter'),('R',  'Relative'),('L',  'Partner')),max_length = 1,default='R')


    class Meta:
        ordering = ('-pk',)

    def __unicode__(self):
        return u'%s' % self.pk

    def get_absolute_url(self):
       return reverse('conform_emergency_detail', args=(self.pk,))

    def get_update_url(self):
        return reverse('conform_emergency_update', args=(self.pk,))


class Client(models.Model):

    # Fields
    surname = CharField(null = False, blank = False, max_length=30)
    name = CharField(null = False, blank = False, max_length=60)

    # Relationship Fields
    emergencycontact = models.ForeignKey(Emergency, on_delete=models.CASCADE, name = 'Emergency Contact')

    class Meta:
        ordering = ('-pk',)

    def __unicode__(self):
        return u'%s' % self.pk

    def get_absolute_url(self):
        return reverse('conform_client_detail', args=(self.pk,))

    def get_update_url(self):
        return reverse('conform_client_update', args=(self.pk,))


class Clientnotes(models.Model):

    # Fields
    slug = AutoSlugField(populate_from='name', blank=True)
    created = DateTimeField(auto_now_add=True, editable=False)
    last_updated = DateTimeField(auto_now=True, editable=False)
    note = CharField(blank=False, null=False, max_length= 300 )

    # Relationship Fields
    modified_by = models.ForeignKey(User, related_name='clientnotes_modified_by', on_delete=models.CASCADE, name= 'Changed by')
    clientnotes = models.ManyToManyField(Client, name = 'Clients notes')

    class Meta:
        ordering = ('-created',)

    def __unicode__(self):
        return u'%s' % self.slug

    def get_absolute_url(self):
        return reverse('conform_clientnotes_detail', args=(self.slug,))

    def get_update_url(self):
        return reverse('conform_clientnotes_update', args=(self.slug,))

形式

class ClientForm(forms.ModelForm):
    class Meta:
        model = Client
        fields = ['surname', 'name']

class ClientnotesForm(forms.ModelForm):
    class Meta:
        model = Clientnotes
        readonly_fields = ['slug', 'modified_by']
        fields = ['note']

VIEWS

class ClientListView(ListView):
    model = Client


class ClientCreateView(CreateView):
    model = Client
    form_class = ClientForm


class ClientDetailView(DetailView):
    model = Client


class ClientUpdateView(UpdateView):
    model = Client
    form_class = ClientForm

模板名字

client_detail.html
client_form.html
client_list.html

我有简单的视图,表单和模板列表,查看详情,并添加,这一切运作良好 - 与相关车型的例外,因为我不能够以两个模型在同一时间增加。 我需要与我有什么设置锁定到位一个简单明确的傻瓜指南。

文章来源: Writing a view to allow user to add related data for a customer, like comments for example; on a single page