Ok, I have make a mistake, the correct is idpedido is only FK. and 
'iditenspedido' is a PK, the correct model is this:

class ItensPedido(models.Model):
    iditenspedido = models.IntegerField('Código', primary_key=True, 
default=-1)
    idpedido = models.ForeignKey('Pedido', db_column='idpedido', default=-1)
    idproduto = models.ForeignKey('Produto', db_column='idproduto')
    preco = models.DecimalField(u'Vlr. Total', max_digits=18, 
decimal_places=4,
                                null=False, default=0)
    quantidade = models.DecimalField(u'Vlr. Total', max_digits=18,
                                     decimal_places=4, null=False, 
default=0)
    precototal = models.DecimalField(u'Vlr. Total', max_digits=18,
                                     decimal_places=4, null=False, 
default=0)


I can't let's django create PK, because, this is a legacy database used by 
your Delphi ERP software, and the table was made on this way ( it's crazy I 
know, but I can't change anything in database now ).

Thanks again.

Em quarta-feira, 21 de agosto de 2013 17h41min45s UTC-3, Daniel Roseman 
escreveu:
>
> On Wednesday, 21 August 2013 20:53:36 UTC+1, Fellipe Henrique wrote:
>
>> Hi,
>>
>> I got this models: http://pastebin.com/w2TmyLzt
>>
>> First: I used db_columns because I used a legacy database from company 
>> ERP.
>>
>> I try to get, in my template the "descricao" field, in my "mercadoria" 
>> model. but I have in my "itenspedido" only "idproduto" not "idmercadoria".
>>
>> My question is: how can I get "descricao" field from "mercadoria" model 
>> in my formset? 
>>
>> I tried {{ itens.idproduto.idmercadoria.descricao }} , {{ 
>> itens.idproduto.idmercadoria.descricao.value }}   and not work.
>>
>> P.S.: I use modelForm as default form, like this:
>>
>> class PedidoItensForm(ModelForm):
>>     class Meta:
>>         model = ItensPedido
>>
>> Thanks,
>>
>> Regards,
>>
>
> You have set `idpedido` as the primary key in the ItensPedido model. 
> Primary keys are not modifiable so do not show up in forms. It makes no 
> sense to that that as the PK in the joining table anyway: PK implies 
> uniqueness, which means you can only have one ItensPedido per Pedido, which 
> defeats the whole purpose of the many-to-many relationship.
>
> Remove the `primary_key=True` attribute and let Django create its own PK 
> column. 
> --
> DR.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to