Field 'id' expected a number but got 'Free'

i’m trying to add a form, so users can post thier own articles , but when i hit publish button it shwos Field 'id' expected a number but got 'Free'. . i wasn’t adding the package_category field to the forms because i have set a default value for it in my models.py

package_category = models.ForeignKey(Package_Category, on_delete=models.DO_NOTHING, verbose_name="Package Category", null=True, default="Free")

when i now add the package_category field to the forms.py fields = [...] it now shows this error Field 'id' expected a number but got 'Free'. . i don’t really know what is going

models.py


class Elements(models.Model):
    title = models.CharField(max_length=10000, null=True, blank=True, verbose_name="Title")
    slug = models.SlugField(unique=True, max_length=1000)
    image = models.ImageField(upload_to="vectors-images/%Y/%m/%d/", default="default.jpg", verbose_name="Image Cover")
    vec_file = models.FileField(upload_to='vector-uploads/%Y/%m/%d/', null=True, blank=True, verbose_name="Upload File")
    category = models.ForeignKey(Category, on_delete=models.DO_NOTHING, verbose_name="Category", default="vector")
    package_category = models.ForeignKey(Package_Category, on_delete=models.DO_NOTHING, verbose_name="Package Category", null=True, default="Free")
    file_format = models.ForeignKey(Format, on_delete=models.SET_NULL, null=True, blank=True, default="EPS")

Views.py

@login_required
def CreateElement(request):
    user = request.user
    categories = Category.objects.all()
    info = Announcements.objects.filter(active=True)
    if request.method == "POST":
        form = CreateElementForm(request.POST, request.FILES)
        if form.is_valid():
            form.instance.creator = request.user
            element = form.save(commit=False)  # ← no commit=False
            element.slug = slugify(element.title)
            # element.package_category == "Free"
            element.save()
            messages.success(request, f'Hi, Your Element have been sent for review and would be live soon!')
            return redirect('creators:dashboard')
    else:
        form = CreateElementForm()

    context = {
        'form': form,
        'info': info,
        'categories': categories
    }
    return render(request, 'newpost.html', context)

error trace

System check identified no issues (0 silenced).
January 09, 2022 - 00:14:08
Django version 3.1, using settings 'dexxapikprj.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Internal Server Error: /elements/create/
Traceback (most recent call last):
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\fields\__init__.py", line 1774, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: 'EPS'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "C:\Users\Destiny\Desktop\dexxapik3\DexxaPik\dexxapikprj\elements\views.py", line 241, in CreateElement
    element.save()
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 750, in save
    self.save_base(using=using, force_insert=force_insert,
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 787, in save_base
    updated = self._save_table(
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 892, in _save_table
    results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 930, in _do_insert
    return manager._insert(
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 1249, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\compiler.py", line 1394, in execute_sql
    for sql, params in self.as_sql():
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\compiler.py", line 1337, in as_sql
    value_rows = [
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\compiler.py", line 1338, in <listcomp>
    [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\compiler.py", line 1338, in <listcomp>
    [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\compiler.py", line 1279, in prepare_value
    value = field.get_db_prep_save(value, connection=self.connection)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\fields\related.py", line 971, in get_db_prep_save
    return self.target_field.get_db_prep_save(value, connection=connection)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\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\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\fields\__init__.py", line 2388, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\fields\__init__.py", line 1776, in get_prep_value
    raise e.__class__(
ValueError: Field 'id' expected a number but got 'EPS'.
[09/Jan/2022 00:14:35] "POST /elements/create/ HTTP/1.1" 500 157932

A ForeignKey contains the primary key of the target table. If the primary key of Package_Category is an integer, then your default value must be that integer.

okay thanks for your reponse, please what can i do now

You can assign an appropriate value for the default.

I can’t be any more specific than that, because you haven’t identified what it is you’re trying to accomplish here.

1 Like

thanks for your response sir, i later figured it out. i was supposed to define a default category function and pass it into

    package_category = models.ForeignKey(..., default="Free")

as

    package_category = models.ForeignKey(..., default=get_package_category)

instead of the string