indianLeo
indianLeo

Reputation: 153

expected string or bytes-like object django while saving to db

I am trying to save my model to db but I keep getting the error expected string or bytes-like object. I tried searching and I thing I am passing it as tuple but I cannot figure out where I made the mistake.

Here is the view of the post route:

class PreviousSearchView(View):
def post(self, request):
    if request.method == 'POST':
        searchdomain = request.POST['searchdomain']
        domain = whois.whois(searchdomain) 
        date = datetime.datetime.now()
        domain_name = domain.domain_name
        creation_date = domain.creation_date
        expiration_date = domain.expiration_date
        org = domain.org
        city = domain.city
        state = domain.state
        zipcode = domain.zipcode
        country = domain.country
        search = Search(
            searchdomain = domain_name,
            org = domain.org,
            city = domain.city,
            state = domain.state,
            zipcode = domain.zipcode,
            country = domain.country,
            user = User.objects.get(id = request.user.id),
            date = date,
            creation_date = domain.creation_date,
            expiration_date = domain.expiration_date,
        )
        search.save() ## This is where the error is coming
    return redirect('/')

Here is the model:

from django.db import models
from django.contrib.auth.models import AbstractUser

# Create your models here.

class User(AbstractUser):
    pass


class Search(models.Model):
    searchdomain = models.CharField(max_length=50)
    creation_date = models.DateField(null=True)
    expiration_date = models.DateField(null=True)
    org = models.CharField(max_length=80, null=True)
    date = models.DateField(null=True)
    city = models.CharField(max_length=30, null=True)
    state = models.CharField(max_length=30, null=True)
    zipcode = models.IntegerField(null=True)
    country = models.CharField(max_length=30, null=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.searchdomain

Here is the full traceback:

Traceback (most recent call last):
  File "C:\Users\indianLeo\.virtualenvs\whois_app-4Kx6o2xE\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\indianLeo\.virtualenvs\whois_app-4Kx6o2xE\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\indianLeo\.virtualenvs\whois_app-4Kx6o2xE\lib\site-packages\django\views\generic\base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\indianLeo\.virtualenvs\whois_app-4Kx6o2xE\lib\site-packages\django\views\generic\base.py", line 98, in dispatch
    return handler(request, *args, **kwargs)
  File "E:\whois_app\whois_app\search\views.py", line 45, in post
    search.save()
  File "C:\Users\indianLeo\.virtualenvs\whois_app-4Kx6o2xE\lib\site-packages\django\db\models\base.py", line 753, in save
    self.save_base(using=using, force_insert=force_insert,
  File "C:\Users\indianLeo\.virtualenvs\whois_app-4Kx6o2xE\lib\site-packages\django\db\models\base.py", line 790, in save_base
    updated = self._save_table(
  File "C:\Users\indianLeo\.virtualenvs\whois_app-4Kx6o2xE\lib\site-packages\django\db\models\base.py", line 895, in _save_table
    results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
  File "C:\Users\indianLeo\.virtualenvs\whois_app-4Kx6o2xE\lib\site-packages\django\db\models\base.py", line 933, in _do_insert
    return manager._insert(
  File "C:\Users\indianLeo\.virtualenvs\whois_app-4Kx6o2xE\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\indianLeo\.virtualenvs\whois_app-4Kx6o2xE\lib\site-packages\django\db\models\query.py", line 1254, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "C:\Users\indianLeo\.virtualenvs\whois_app-4Kx6o2xE\lib\site-packages\django\db\models\sql\compiler.py", line 1396, in execute_sql
    for sql, params in self.as_sql():
  File "C:\Users\indianLeo\.virtualenvs\whois_app-4Kx6o2xE\lib\site-packages\django\db\models\sql\compiler.py", line 1339, in as_sql
    value_rows = [
  File "C:\Users\indianLeo\.virtualenvs\whois_app-4Kx6o2xE\lib\site-packages\django\db\models\sql\compiler.py", line 1340, in <listcomp>
    [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
  File "C:\Users\indianLeo\.virtualenvs\whois_app-4Kx6o2xE\lib\site-packages\django\db\models\sql\compiler.py", line 1340, in <listcomp>
    [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
  File "C:\Users\indianLeo\.virtualenvs\whois_app-4Kx6o2xE\lib\site-packages\django\db\models\sql\compiler.py", line 1281, in prepare_value
    value = field.get_db_prep_save(value, connection=self.connection)
  File "C:\Users\indianLeo\.virtualenvs\whois_app-4Kx6o2xE\lib\site-packages\django\db\models\fields\__init__.py", line 823, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "C:\Users\indianLeo\.virtualenvs\whois_app-4Kx6o2xE\lib\site-packages\django\db\models\fields\__init__.py", line 1222, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Users\indianLeo\.virtualenvs\whois_app-4Kx6o2xE\lib\site-packages\django\db\models\fields\__init__.py", line 1217, in get_prep_value
    return self.to_python(value)
  File "C:\Users\indianLeo\.virtualenvs\whois_app-4Kx6o2xE\lib\site-packages\django\db\models\fields\__init__.py", line 1179, in to_python
    parsed = parse_date(value)
  File "C:\Users\indianLeo\.virtualenvs\whois_app-4Kx6o2xE\lib\site-packages\django\utils\dateparse.py", line 75, in parse_date
    match = date_re.match(value)
TypeError: expected string or bytes-like object

Also I tried removing the date field and migrated but it still did not work. I am also adding the form I am rendering.

class SearchForm(forms.ModelForm):
    class Meta:
        model = Search
        fields = '__all__'
        exclude = ('date', 'user', 'city', 'state', 'country', 'zipcode', 'expiration_date', 'creation_date', 'org')

Upvotes: 1

Views: 1805

Answers (1)

ImTryingMyBest
ImTryingMyBest

Reputation: 372

I can't really duplicate this exact error in my own environment, but my inclination is that it has to do with the database engine. I've see similar errors in the past with databases, as well as with json requests/responses.

To understand the TypeError: expected string or bytes-like object, its basically telling you that its looking for something that looks like the result of the json.dumps() output, if you're familiar with that. In this case, its getting a datetime.datetime object and doesn't know what to do with it by the looks of your traceback.

try to use this, and if it doesn't work, make sure that you can cast that type to a str() using another method as discussed in this thread:


date = str(datetime.datetime.now())

Upvotes: 2

Related Questions