Add lines to tabular-inline, after saving... django-admin -
first of want thank in advance provide global community, invaluable service. write code hobby, , want finish project (my first) have personal goal , serve nonprofit organization, find myself in somehow awkward position, since english not native language, finding answer (which on stackoverflow) problem me. first question ever... hope well. btw django it's fantastic... have these 2 models:
# models.py class cabmovimiento(infocomun): """ guarda información de la cabecera de los movimientos internos de equipos, salvo préstamos. hereda de infocomun los campos. """ nro = models.integerfield( default=lambda: ult_nro_mov('cm'), verbose_name="número", blank=true) det = models.manytomanyfield( 'inventario.equipo', through='detmov', verbose_name="componente") class meta: verbose_name = 'movimiento interno' verbose_name_plural = 'movimientos internos' def los_detalles(self): return "\n".join([d.serie d in self.det.all().filter(self.id)]) los_detalles.short_description = "detalles" class detmov(models.model): """ guarda el detalle de cada documento generado por los movimientos internos, excepción de los préstamos. """ cab = models.foreignkey('cabmovimiento') eqp = models.foreignkey('inventario.equipo', verbose_name='equipo/s') tip_mov = models.foreignkey( 'inventario.tmetl', limit_choices_to={ 'asociacion': 'e', 'tipo': 'm' }, verbose_name='tipo de movimiento') for_mov = models.charfield( max_length=1, choices=fmov, default='u', verbose_name='forma movimiento') diag = models.charfield(max_length=80, verbose_name='diagnóstico') class meta: verbose_name = 'detalle de movimiento' verbose_name_plural = 'detalles de movimientos'
i have read , reread documentation of django (1.5) signals, save , save_related methods, have followed several tutorials here , in other no less interesting blogs, can not want .... need detect if for_mov field, part of detmov, changes value, similar status field refers way of moving , may 'u', one, or 'c' complete, refers set of something.
once detected , if simple movement('u'), must ignore change or addition, in case of complete movement ('c'), must search database , add more rows tabular-inline , save it. know how queries, not know how add them ... think i'll have use raw sql, though less elegant, not discarded.
sorry if it's simple, , sorry if not understand want, since i'm using gtranslate. tia.
one potential solution override save()
method on detmov
fetched latest value of , check if it's changed.
def save(self, *args, **kwargs): if self.id , self.for_mov == 'c': stored_for_mov = detmov.objects.get(id=self.id).values_list('for_mov', flat=true)[0] if stored_for_mov != self.for_mov: # stored value isn't 'c' # add other records need to. super(detmov, self).save(*args, **kwargs)
i'm not quite sure if need prevent save in case of 'u'. didn't follow part, should solve problem you're having. pull post_save
signal well.
Comments
Post a Comment