python - Add elements to inlineformset_factory using the shell -
i need add elements of invoice details using shell inlineformset_factory, models are:
class invoicedetail(createupdatemixin): client = models.foreignkey('client.client', verbose_name=_('factura a'), blank=true, null=true, default=none) date = models.datefield() tax = models.floatfield() concept = models.foreignkey(conceptdetail, null=true, blank=true, verbose_name=_('clave concepto')) custom_concept = models.charfield(max_length=150, null=true, blank=true, verbose_name=_('concepto')) amount = models.integerfield(verbose_name=_('cantidad')) fee = models.floatfield(verbose_name=_('honorarios')) duty = models.floatfield(verbose_name=_('gastos')) invoice = models.foreignkey(invoice, null=true, blank=true, related_name='detail_concepts') class invoice(createupdatemixin): invoice = models.charfield(max_length=15, verbose_name=_('nĂºmero de factura')) # details show when calls invoice choice menu in procedure # mabe addenda title_addenda = models.charfield(max_length=80, verbose_name=_('titulo detalle factura')) detail_addenda = models.charfield(max_length=350, verbose_name=_('detalle factura')) status = models.foreignkey(statusinvoice, null=true, blank=true)
i tried it, doesn't work
in [54]: invoice_obj = invoice.objects.get(invoice=100067) out[55]: <invoice: 100067> in [56]: inv_det = invoicedetail.objects.get(custom_concept='professional fees payment ofeach annual tax on patents') out[57]: <invoicedetail: b'professional fees payment ofeach annual tax on patents'> in [58]: # create inlineformset_factory in [59]: testformset = inlineformset_factory(invoice, invoicedetail, fields='__all__') in [61]: test = testformset(instance=invoice_obj) in [62]: test.data out[62]: {} in [63]: test.save() out[63]: []
i try follow django docs inline-formsets according docs steps right, i'm doing wrong?
edited add more details
i have migration script in need add multiples invoicedetail objects invoice, reason try convert inlineformset_factory thinking right way don't know, being if added directly invoicedetail invoice doesn't maintain invoicedetail preserves latest, if modify model invoice m2m invoicedetails can preserve elements can't use formset.all invoicedetail preserves latest.
here script code:
def _get_invoice_detail(self, key, complex_dictionary): """ add correct data need create formset so. :param complex_dictionary: :return: """ # django.forms.models import inlineformset_factory # invoicedetailformset = inlineformset_factory(invoice, invoicedetail, fields='__all__') # load invoices db _db = {} _concept = {} obj = none clase = none in invoice.objects.all(): _db[i.invoice] = res = {} in conceptdetail.objects.all(): _concept[i.pk] = in complex_dictionary: try: if int(i['cclave']) in self.clients: _client = self.clients[int(i['cclave'])] _client = client.objects.get(pk=int(i['cclave'])) except: _client = self._client_default try: _date = datetime.date(int(i['fecha'][0:4]), int(i['fecha'][4:6]), int(i['fecha'][6:8])) except: _date = datetime.date(1970, 1, 1) # "invoice" instance isn't saved in database. if i['factura'] in _db: obj = _db[i['factura']] else: try: obj = invoice.objects.get(invoice=i['factura']) except: exc_type, exc_value, exc_traceback = sys.exc_info() print('error getting invoice object:', key, exc_value, repr(traceback.format_exception(exc_type, exc_value, exc_traceback))) if i['clvart'] in _concept: clase = _concept[i['clvart']] else: try: clase = conceptdetail.objects.get(pk=i['clvart']) except: exc_type, exc_value, exc_traceback = sys.exc_info() print('error getting conceptdetail object:', i['clvart'], exc_value, repr(traceback.format_exception(exc_type, exc_value, exc_traceback))) invoice_detail = { 'client': _client, 'date': _date, 'tax': int(i['iva']), 'concept': clase, 'custom_concept': i['deart1']+i['deart2']+i['deart3']+i['deart4'], 'amount': i['cantidad'], 'fee': i['honorario'], 'duty': i['derechos'], 'invoice': obj } try: if i[key] in res: res[i[key]].append(invoicedetail.objects.update_or_create(**invoice_detail)[0]) else: res[i[key]] = [invoicedetail.objects.update_or_create(**invoice_detail)[0]] except: exc_type, exc_value, exc_traceback = sys.exc_info() print('error creating invoice detail:', invoice_detail, exc_value, repr(traceback.format_exception(exc_type, exc_value, exc_traceback))) def create_invoice_details(self, m): _invoice_detail = jdft(m.fetch_dbase_table('factpan.dbf')).as_frame('factura')\ .drop(['nombre', 'desc', 'precio', 'subtot', 'toiva', 'relanex', 'rfc', 'lastup'], 1)\ .to_dict(orient='records') self._get_invoice_detail('factura', _invoice_detail)
Comments
Post a Comment