Re: [Python] Django question
Mozione d'ordine: io scusate ma proprio non riesco a leggere il codice postato nelle mail e - purtroppo - ignoro tutte o quasi le mail che contengono più di due righe di codice. Possiamo rendere obbligatorio l'uso di un pastebin? Pace, R: ___ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python
Re: [Python] Django question
Il 12 maggio 2015 11:43, Simone Federici s.feder...@gmail.com ha scritto: Non non possiamo renderlo obbligatorio, ma possiamo continuare a ignorare le mail che non lo usano. Che dici? Però mi dispiace... vorrei contribuire ;) Pace, R ___ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python
Re: [Python] Django question
Roberto Polli: Mozione d'ordine: io scusate ma proprio non riesco a leggere il codice postato nelle mail e - purtroppo - ignoro tutte o quasi le mail che contengono più di due righe di codice. Possiamo rendere obbligatorio l'uso di un pastebin? Ahaha le ignoro anche io, infatti sto a rispondere a te anziché al vero thread. Non non possiamo renderlo obbligatorio, ma possiamo continuare a ignorare le mail che non lo usano. Che dici? ___ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python
[Python] Django question
Mi sono arenato (spiaggiato tipo megattera se non perdo qualche chilo) con la get_or_create. model class Routers(models.Model): uuid = models.CharField(max_length=50,unique=True) mac_address = models.CharField(max_length=50,unique=True) hosters_address_id = models.ForeignKey(hosters_manage.HostersAddress) router_pubblic_ip = models.CharField(max_length=20) logged_users = models.IntegerField(default=0) activation_date = models.DateTimeField(auto_now_add=True,null=True,blank=True) active = models.BooleanField(default=False) view try: hoster_address = HostersAddress.objects.get(pk=data['hosters_address_id']) new_router, created = Routers.objects.get_or_create( { 'uuid': data['uuid'], 'mac_address': data['mac_address'], 'hosters_address_id': hoster_address, 'router_pubblic_ip': data['router_pubblic_ip'], 'active': data['active'] }, defaults={ 'uuid': data['uuid'], 'mac_address': data['mac_address'], }) if created == True: new_router.save() except Exception, ex: print ex Viene invocata in sequnza con parametri sempre diversi e dopo avere inserito il primo mi torna get_or_create() got multiple values for keyword argument 'defaults' Qualche idea? Googolare non mi ha ha portato a nulla Gracias Carlos -- EZLN ... Para Todos Todo ... Nada para nosotros ___ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python
Re: [Python] Django question
2015-05-11 15:52 GMT+02:00 Carlos Catucci carlos.catu...@gmail.com: [snip] hoster_address = HostersAddress.objects.get(pk=data['hosters_address_id']) new_router, created = Routers.objects.get_or_create( { 'uuid': data['uuid'], 'mac_address': data['mac_address'], 'hosters_address_id': hoster_address, 'router_pubblic_ip': data['router_pubblic_ip'], 'active': data['active'] }, defaults={ 'uuid': data['uuid'], 'mac_address': data['mac_address'], }) perchè uuid e mac_address li hai indicati in entrambe le liste? forse intendevi questo? { 'uuid': data['uuid'], 'mac_address': data['mac_address'], }, defaults={ 'hosters_address_id': hoster_address, 'router_pubblic_ip': data['router_pubblic_ip'], 'active': data['active'] }) ossia cerchi per uuid e mac_address e solo nel caso in cui django debba crearlo allora specifichi anche hosters_address_id, router_pubblic_ip e active può essere? [snip] -- Marco ___ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python
Re: [Python] Django question
2015-05-11 16:12 GMT+02:00 Marco De Paoli depao...@gmail.com: ossia cerchi per uuid e mac_address e solo nel caso in cui django debba crearlo allora specifichi anche hosters_address_id, router_pubblic_ip e active può essere? in effetti provo e ti dico Carlos -- EZLN ... Para Todos Todo ... Nada para nosotros ___ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python
Re: [Python] Django question
2015-05-11 16:51 GMT+02:00 Carlos Catucci carlos.catu...@gmail.com: 2015-05-11 16:46 GMT+02:00 Carlos Catucci carlos.catu...@gmail.com: in effetti provo e ti dico provato ma mi da sempre lo stesso errore. Premetto che ho controllato e non ci sono array o valori multipli nei parametri. Non riesco a capire dove possa essere il problema. Ecco il codice completo, magari auta def add_new_router(request): retval = None if 'data' in request.POST: data = json.loads(request.POST.get('data', '')) try: hoster_address = HostersAddress.objects.filter(pk=data['hosters_address_id']) new_router, created = Routers.objects.get_or_create( { 'mac_address': data['mac_address'], 'uuid': data['uuid'], }, acc, qui mac_address e uuid li stai passando dentro un dizionario e invece sono dei kwargs https://docs.djangoproject.com/en/1.8/ref/models/querysets/ prova nel formato: Routers.objects.get_or_create( mac_address=data['mac_address'], uuid=data['uuid'], defaults={ 'hosters_address_id': hoster_address[0].id, 'router_pubblic_ip': data['router_pubblic_ip'], 'active': data['active'] }) if created == True: new_router.save() retval = 'OK' except Exception, ex: print ex retval = ex return HttpResponse(retval) def fill_routers(request): for hoster_address in HostersAddress.objects.filter(active=True,deleted=False): mutable = request.POST._mutable request.POST._mutable = True request.POST['data'] = json.dumps({ 'mac_address': generate_mac_addr(), 'uuid': generate_uuid(), 'hosters_address_id': hoster_address.id, 'router_pubblic_ip': generate_ip(), 'active': True, }) request.POST._mutable = mutable new_router = add_new_router(request) return HttpResponse('OK') La fill_routers chiama la funzione dove poi e' contenuta la get_or_create. Carlos -- EZLN ... Para Todos Todo ... Nada para nosotros ___ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python ___ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python
Re: [Python] Django question
2015-05-11 16:46 GMT+02:00 Carlos Catucci carlos.catu...@gmail.com: in effetti provo e ti dico provato ma mi da sempre lo stesso errore. Premetto che ho controllato e non ci sono array o valori multipli nei parametri. Non riesco a capire dove possa essere il problema. Ecco il codice completo, magari auta def add_new_router(request): retval = None if 'data' in request.POST: data = json.loads(request.POST.get('data', '')) try: hoster_address = HostersAddress.objects.filter(pk=data['hosters_address_id']) new_router, created = Routers.objects.get_or_create( { 'mac_address': data['mac_address'], 'uuid': data['uuid'], }, defaults={ 'hosters_address_id': hoster_address[0].id, 'router_pubblic_ip': data['router_pubblic_ip'], 'active': data['active'] }) if created == True: new_router.save() retval = 'OK' except Exception, ex: print ex retval = ex return HttpResponse(retval) def fill_routers(request): for hoster_address in HostersAddress.objects.filter(active=True,deleted=False): mutable = request.POST._mutable request.POST._mutable = True request.POST['data'] = json.dumps({ 'mac_address': generate_mac_addr(), 'uuid': generate_uuid(), 'hosters_address_id': hoster_address.id, 'router_pubblic_ip': generate_ip(), 'active': True, }) request.POST._mutable = mutable new_router = add_new_router(request) return HttpResponse('OK') La fill_routers chiama la funzione dove poi e' contenuta la get_or_create. Carlos -- EZLN ... Para Todos Todo ... Nada para nosotros ___ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python
Re: [Python] Django question
Ciao, Il 11/05/2015 16:51, Carlos Catucci ha scritto: try: hoster_address = HostersAddress.objects.filter(pk=data['hosters_address_id']) new_router, created = Routers.objects.get_or_create( { 'mac_address': data['mac_address'], 'uuid': data['uuid'], }, defaults={ 'hosters_address_id': hoster_address[0].id, 'router_pubblic_ip': data['router_pubblic_ip'], 'active': data['active'] }) if created == True: new_router.save() leggi la doc per favore, stai salvando un oggetto appena creato per niente, di solito si tende a fare il contrario cioè aggiornare un oggetto se non è stato creato retval = 'OK' except Exception, ex: print ex retval = ex un Exception piglia tutto è un anti-pattern, piuttosto se hoster_address non torna niente non fare niente. -- Riccardo Magliocchetti @rmistaken http://menodizero.it ___ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python