在Django中保存重复的记录

在Django中保存重复的记录

本文介绍了在Django中保存重复的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习django,我有这个新手问题:
我必须建模模型之间的关系是一对多。一个提供者必须有很多DetailLoad。

I starting to learn django and I have this newbie issue:I have to models the relation between the model is one to many. One provider must have many DetailLoad.

我为DetailLoad创建一个表单,我只需要保存id_proveedor(FK),anio和mes。
但是当我尝试保存表单时,验证错误将显示详细信息此提供程序的加载程序已存在

I create a form for the DetailLoad where I only need to save the id_proveedor (FK), anio and mes.But when I try to save the form a validation error is displayed "detail Load for this provider already exists".

模型


$ b

Models.py

    class Provider(models.Model):
        id_proveedor = models.AutoField(primary_key=True, db_column='id_proveedor')
        nombre = models.CharField(max_length=50, blank=True)
        tipo = models.CharField(max_length=50, blank=True)
        asignado_a = models.CharField(max_length=15, blank=True, verbose_name='Asignado a')
        moneda = models.CharField(max_length=1, blank=True)
        class Meta:
            managed = False
            db_table = 'mpc_proveedores'

        def __unicode__(self):
            return self.nombre

class DetailLoad(models.Model):
    id_proveedor = models.ForeignKey(Proveedor,db_column='id_proveedor', primary_key=True, verbose_name='Proveedor')
    anio = models.IntegerField(blank=True, null=True)
    mes = models.IntegerField(blank=True, null=True)
    f_carga = models.DateField(blank=True, null=True, verbose_name='Fecha de carga')
    usuario = models.CharField(max_length=15, blank=True)
    registros = models.FloatField(blank=True, null=True,verbose_name='Cant. de Registros')
    num_codigos = models.IntegerField(blank=True, null=True,verbose_name='Cant. codigos')
    lista_codigos = models.CharField(max_length=2500, blank=True, verbose_name='Lista de Codigos')
    class Meta:
        managed = False
        db_table = 'mpc_detalle_carga_archivo'

我用于保存数据的表单的定义:

the definition of the form that I use to save the data:

class NewDetLoadForm(forms.ModelForm):
      def __init__(self,*args,**kwargs):
       super(NewDetRecForm,self).__init__(*args,**kwargs)
       self.helper = FormHelper(self)
       self.helper.layout.append(Submit('save','Grabar'))
       self.helper.layout = Layout(
        Fieldset('',
                 'id_proveedor',
                 'anio',
                 'mes',
                 Submit('save','Grabar'),
                )
       )


      class Meta:
        model = DetalleRecepcion

我的观点:

def NewDetLoadView(request):
    if request.method == "POST":
       drform = NewDetLoadForm(request.POST or none)
       if drform.is_valid():
          drform.save()
          return HttpResponseRedirect('/monitor/')
    else:
       drform = NewDetLoadForm()

    return render_to_response('newdetload.html',locals(),
                               context_instance=RequestContext(request))

我需要能够保存记录,选择提供者并填充其他两个字段,即使是重复的行。
顺便说一下,遗留数据库,这些表没有约束或任何种类的关系另一个表。

I need to be capable to save records, selecting the provider and filling the other two fields even if are duplicate rows.By the way is a legacy database, the tables doesn't have constraints or any kind of relationship another tables.

提前感谢

推荐答案

DetailLoad 模型中,您正在设置 Proveedor ForeignKey,也使它成为主键。主键始终是唯一的,这就是为什么您不能添加另一个条目。您需要从

In the DetailLoad model you are setting Proveedor ForeignKey and also making it a primary key. The primary keys are always unique that is why you can not add another entry. You need to remove the primary_key=True from:

id_proveedor = models.ForeignKey(Proveedor,db_column='id_proveedor', primary_key=True, verbose_name='Proveedor')

在django中,主键被自动生成,您不需要在模型中定义它们。主键可以使用 id pk 属性访问。

In django the primary keys are automatically generated and you don't need to define them in your models. The primary keys can be accessed with id or pk attribute.

这篇关于在Django中保存重复的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 04:00