Mam problemy z metodą Django's {X0}} na modelu poniżej. Otrzymuję ten błąd:

ValueError: Nieprawidłowy dosłowny dla INT () z podstawą 10: ''

Błąd wydaje się zawsze rzucać ostatnią linią update_or_create, ponieważ kiedy dodałem verified=False (który ma domyślną wartość False) do instrukcji, błąd śledził z powrotem do linii za pomocą {{{ X2}} zamiast linii pokazanej poniżej.

Moje modele:

class Addresses(models.Model):
    customer = models.ForeignKey('Customers', on_delete=models.CASCADE)
    address_name = models.CharField(max_length=45)
    company = models.CharField(max_length=45)
    street1 = models.CharField(max_length=60)
    street2 = models.CharField(max_length=30, blank=True, null=True)
    street3 = models.CharField(max_length=30, blank=True, null=True)
    city = models.CharField(max_length=45)
    state = models.CharField(max_length=45)
    zip = models.IntegerField()
    country = models.CharField(default='US', max_length=45)
    phone = models.IntegerField(blank=True, null=True)
    residential = models.BooleanField(default=True)
    verified = models.BooleanField(default=False)

class Customers(models.Model):
    customer_name = models.CharField(max_length=75)
    email = models.CharField(max_length=60, blank=True, null=True)
    phone = models.IntegerField(blank=True, null=True)
    ss_customer_id = models.IntegerField(blank=True, null=True, unique=True)

Funkcja:

def addAddress(self, address, customer_id):
    if address['name'] is not None and address['street1'] is not None and address['city'] is not None:
        if address['state'] is not None and address['postalCode'] is not None:

            address, created = Addresses.objects.update_or_create(
                address_name=address['name'],
                company=address['company'],
                street1=address['street1'],
                street2=address['street2'],
                street3=address['street3'],
                city=address['city'],
                state=address['state'],
                country=address['country'],
                phone=address['phone'],
                customer=customer_id #error thrown here
            )

            address.save()
            return address
    return False

Dane:

address = {
    'name': 'Johnny Appleseed',
    'company': 'Apple Inc',
    'street1': '456 Street',
    'street2': 'APT 12',
    'street3': None,
    'city': 'City',
    'state': 'LA',
    'postalCode': '90000',
    'country': 'US',
    'phone': '4444444444',
}
customer_id = 2 #this is a valid primaryKey

self.addAddress(address, customer_id)

To jest pełny Traceback:

Traceback (most recent call last):
  File "/Users/jane/Code/dbmu-env/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/jane/Code/dbmu-env/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/jane/Code/dbmu-env/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/jane/Code/dbmu-env/env/lib/python3.7/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/jane/Code/dbmu-env/env/lib/python3.7/site-packages/django/utils/decorators.py", line 45, in _wrapper
    return bound_method(*args, **kwargs)
  File "/Users/jane/Code/dbmu-env/env/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/jane/Code/dbmu-env/env/lib/python3.7/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/Users/jane/Code/dbmu-env/dbmu/webhooks.py", line 39, in post
    self.process_orders(orders)
  File "/Users/jane/Code/dbmu-env/dbmu/webhooks.py", line 57, in process_orders
    shipping = self.addAddress(order['shipTo'], customer_id)
  File "/Users/jane/Code/dbmu-env/dbmu/webhooks.py", line 88, in addAddress
    verified=False,
  File "/Users/jane/Code/dbmu-env/env/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/jane/Code/dbmu-env/env/lib/python3.7/site-packages/django/db/models/query.py", line 502, in update_or_create
    obj = self.select_for_update().get(**lookup)
  File "/Users/jane/Code/dbmu-env/env/lib/python3.7/site-packages/django/db/models/query.py", line 390, in get
    clone = self.filter(*args, **kwargs)
  File "/Users/jane/Code/dbmu-env/env/lib/python3.7/site-packages/django/db/models/query.py", line 844, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/Users/jane/Code/dbmu-env/env/lib/python3.7/site-packages/django/db/models/query.py", line 862, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/Users/jane/Code/dbmu-env/env/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1263, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "/Users/jane/Code/dbmu-env/env/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1287, in _add_q
    split_subq=split_subq,
  File "/Users/jane/Code/dbmu-env/env/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1225, in build_filter
    condition = self.build_lookup(lookups, col, value)
  File "/Users/jane/Code/dbmu-env/env/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1096, in build_lookup
    lookup = lookup_class(lhs, rhs)
  File "/Users/jane/Code/dbmu-env/env/lib/python3.7/site-packages/django/db/models/lookups.py", line 20, in __init__
    self.rhs = self.get_prep_lookup()
  File "/Users/jane/Code/dbmu-env/env/lib/python3.7/site-packages/django/db/models/lookups.py", line 70, in get_prep_lookup
    return self.lhs.output_field.get_prep_value(self.rhs)
  File "/Users/jane/Code/dbmu-env/env/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 1807, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: ''

Aktualizacja

Próbowałem tego i nadal uzyskać ten sam błąd:

def addAddress(self, address, customer_id):
    ...
    customer = Customers.objects.get(pk=customer_id)
    print(customer) #Customers object (2)

    address, created = Addresses.objects.update_or_create(
    ...

    customer=customer,
    )

Co ja robię źle? Każda rada byłaby doceniana :)

0
Reinstate Monica 1 marzec 2019, 00:32

2 odpowiedzi

Najlepsza odpowiedź

Adres.Customer jest obiektem, a nie zwykłym identyfikatorem. Aby go wypełnić, musisz go pobrać z bazy danych. W związku z tym:

 Customer.objects.get(customer_id)

Mam nadzieję, że to pomoże.

1
hd1 28 luty 2019, 21:36

Dzięki @ HD1 udało mi się to rozgryźć. Miałem 3 rzeczy nie tak z Addresses.objects.update_or_create

  • Przechodziłem do klucza podstawowego klienta zamiast obiektu klienta (odpowiedź HD1)

  • Nie wyznaczyłem wartości dla wymaganego pola zip

  • Kolumna numeru telefonu przekroczyła maksymalną długość dla jego wartości

Z jakiegoś powodu wszystkie te rzeczy spowodowały błąd w moim pytaniu, który ma zostać rzucony!

Po tym, jak naprawiłem wszystkie z nich przestałem uzyskać błędy.

0
Elrond Supports Monica 28 luty 2019, 22:13