ProgrammingError: SELECT COUNT(*) AS "__count" FROM "product" - python

I am using multiple databases in Django and connected default SQLite and PostgreSQL db in the settings.py.
setting.py :
DATABASE_ROUTERS = ['routers.db_routers.AppRouter']
DATABASE_APPS_MAPPING = {'product': 'postgres',}
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
},
'postgres': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'product',
'USER': 'postgres',
'PASSWORD':'password',
'HOST':'localhost'
}
}
And also made the db_routers.py in the routers folder:
class AppRouter:
"""
A router to control all database operations on models in the
product application.
"""
def db_for_read(self, model, **hints):
"""
Attempts to read user models go to postgres.
"""
if model._meta.app_label == 'product':
return 'postgres'
return 'default'
def db_for_write(self, model, **hints):
"""
Attempts to write user models go to postgres.
"""
if model._meta.app_label == 'product':
return 'postgres'
return 'default'
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations if a model in the user app is involved.
"""
if obj1._meta.app_label == 'product' or \
obj2._meta.app_label == 'product':
return True
elif 'product' not in [obj1._meta.app_label, obj2._meta.app_label]:
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
Make sure the auth app only appears in the 'product_db'
database.
"""
if app_label == 'product':
return db == 'postgres'
return None
here, it's model.py:
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField(max_digits=10, decimal_places=2)
weight = models.DecimalField(max_digits=10, decimal_places=2)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
app_label = 'product'
def __str__(self):
return self.name
I have successfully made the tables by running python3 manage.py makemigrations but when I try to migrate using python3 manage.py migrate --database=postgres, I am getting this error: ProgrammingError: relation "product" does not exist. SELECT COUNT(*) AS "__count" FROM "product"And the table is also not present in PgAdmin.
migration.py of product:
# Generated by Django 3.2.5 on 2021-07-16 13:25
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Product',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('price', models.DecimalField(decimal_places=2, max_digits=10)),
('weight', models.DecimalField(decimal_places=2, max_digits=10)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
],
options={
'db_table': 'product',
'managed': False,
},
),
]
Error:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/api/product/
Django Version: 3.2.5
Python Version: 3.8.10
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken',
'post',
'product']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
The above exception (relation "product" does not exist
LINE 1: ...product"."created_at", "product"."updated_at" FROM "product"
^
) was the direct cause of the following exception:
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/rest_framework/generics.py", line 239, in get
return self.list(request, *args, **kwargs)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/rest_framework/mixins.py", line 46, in list
return Response(serializer.data)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/rest_framework/serializers.py", line 745, in data
ret = super().data
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/rest_framework/serializers.py", line 246, in data
self._data = self.to_representation(self.instance)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/rest_framework/serializers.py", line 663, in to_representation
return [
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/db/models/query.py", line 280, in __iter__
self._fetch_all()
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/db/models/query.py", line 1324, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/db/models/query.py", line 51, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1175, in execute_sql
cursor.execute(sql, params)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 98, in execute
return super().execute(sql, params)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/rudrakshi/Desktop/Rose/assignment/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
Exception Type: ProgrammingError at /api/product/
Exception Value: relation "product" does not exist
LINE 1: ...product"."created_at", "product"."updated_at" FROM "product"
^
After running makemigrations and then migrate,
I run my server and when I hit the endpoint- 'api/product/' I got this error - relation "product" does not exist LINE 1: ...product"."created_at", "product"."updated_at" FROM "product"

Looking at your migration file you have 'managed': False,, what does this mean? It means that you don't want the migration system to manage this model. This means that running migrate will not cause the corresponding tables to be created in the database. Don't edit migration files unless you understand what you are doing!
Remove whatever changes you might have made to the migration file, especially that 'managed': False, and then run:
python manage.py migrate product zero --fake
This will cause Django to mark all migrations for the app "product" as unapplied. This is fine since it seems like this is your initial migration, otherwise you might have wanted to change zero to some migration name. Next run:
python manage.py migrate

Related

Django administration Site. 'Delete multiple objects' produces ValidationError

When attempting to delete multiple rows, which have a one to one relationship with two other tables I received the following error:
['“March 21, 2022” value has an invalid date format. It must be in YYYY-MM-DD format.']
The models are set up as such:
class Media(models.Model):
date = models.DateField(primary_key=True, unique=True)
received_url = models.CharField(max_length=200, blank=True, null=True)
class Api(models.Model):
media = models.OneToOneField(Media, on_delete=models.CASCADE)
request_url = models.CharField(max_length=200)
class Text(models.Model):
media = models.OneToOneField(Media, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
copyright = models.CharField(max_length=200, blank=True, null=True)
I am trying to delete the items in the Home › My_App_Main › Medias › Delete multiple objects from the Django administration site. Which presents me with the following:
Are you sure?
Are you sure you want to delete the selected medias? All of the following objects and their related items will be deleted:
Summary
Medias: 2
Apis: 2
Texts: 2
Objects
Media: 2022-03-21
Api: 2022-03-21
Text: 2022-03-21
Media: 2022-03-20
Api: 2022-03-20
Text: 2022-03-20
I then click Yes, I'm Sure
Which triggers then triggers the error.
Checking the POST request in the network log for the browser I noted the dates appear to be in the wrong format:
_selected_action […]
0 "March+21,+2022"
1 "March+20,+2022"
action "delete_selected"
post "yes"
I tried in both Firefox and Chrome.
Checking the data from the Django shell nothing stands out as incorrect. The data comes back as expected:
>>> for field in Media.objects.all():
... print(f'{type(field.date)}({field.date})')
...
<class 'datetime.date'>(2022-03-21)
<class 'datetime.date'>(2022-03-20)
>>> for field in Text.objects.all():
... print(f'{type(field.media.date)}({field.media.date})')
...
<class 'datetime.date'>(2022-03-21)
<class 'datetime.date'>(2022-03-20)
>>> for field in Api.objects.all():
... print(f'{type(field.media.date)}({field.media.date})')
...
<class 'datetime.date'>(2022-03-21)
<class 'datetime.date'>(2022-03-20)
It seems to me it could something to do with the Django administration site doing something odd with the date format? Or perhaps it's the browser defaults that are the issue? Or maybe even something I need to set in the Django settings file? If that's the case how would one go about changing that behaviour?
The error as it comes back in the browser:
ValidationError at /admin/my_app_main/media/
['“March 21, 2022” value has an invalid date format. It must be in YYYY-MM-DD format.']
Request Method: POST
Request URL: http://127.0.0.1:11111/admin/my_app_main/media/
Django Version: 4.0.3
Exception Type: ValidationError
Exception Value:
['“March 21, 2022” value has an invalid date format. It must be in YYYY-MM-DD format.']
Exception Location: /home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/fields/init.py, line 1370, in to_python
Python Executable: /home/user/.pyenv/versions/my_app/bin/python
Python Version: 3.8.2
Python Path:
['/home/user/git/user/my_app',
'/home/user/git/user/my_app',
'/home/user/.vscode/extensions/ms-python.python-2022.2.1924087327/pythonFiles/lib/python/debugpy/_vendored/pydevd',
'/home/user/.pyenv/versions/3.8.2/lib/python38.zip',
'/home/user/.pyenv/versions/3.8.2/lib/python3.8',
'/home/user/.pyenv/versions/3.8.2/lib/python3.8/lib-dynload',
'/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages',
'/home/user/git/user/my_app/apps']
Server time: Mon, 21 Mar 2022 14:40:59 +0000
Traceback:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:11111/admin/my_app_main/media/
Django Version: 4.0.3
Python Version: 3.8.2
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_extensions',
'apps.my_app_main.apps.MyAppMainConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/contrib/admin/options.py", line 683, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/utils/decorators.py", line 133, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/views/decorators/cache.py", line 62, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/contrib/admin/sites.py", line 242, in inner
return view(request, *args, **kwargs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/utils/decorators.py", line 46, in _wrapper
return bound_method(*args, **kwargs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/utils/decorators.py", line 133, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/contrib/admin/options.py", line 1983, in changelist_view
response = self.response_action(
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/contrib/admin/options.py", line 1586, in response_action
queryset = queryset.filter(pk__in=selected)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/query.py", line 1071, in filter
return self._filter_or_exclude(False, args, kwargs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/query.py", line 1089, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, args, kwargs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/query.py", line 1096, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1466, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1496, in _add_q
child_clause, needed_inner = self.build_filter(
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1412, in build_filter
condition = self.build_lookup(lookups, col, value)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1242, in build_lookup
lookup = lookup_class(lhs, rhs)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/lookups.py", line 27, in __init__
self.rhs = self.get_prep_lookup()
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/lookups.py", line 275, in get_prep_lookup
rhs_value = self.lhs.output_field.get_prep_value(rhs_value)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1404, in get_prep_value
return self.to_python(value)
File "/home/user/.pyenv/versions/my_app/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1370, in to_python
raise exceptions.ValidationError(
Exception Type: ValidationError at /admin/my_app_main/media/
Exception Value: ['“March 21, 2022” value has an invalid date format. It must be in YYYY-MM-DD format.']
I would recommend not using DateField as a primary key. Or is there any reason to do that? You can keep unique=True anyway if you want to check there are no two instances with the same date.
class Media(models.Model):
date = models.DateField(unique=True)
received_url = models.CharField(max_length=200, blank=True, null=True)

Manager isn't available; 'auth.User' has been swapped for 'accounts.User'

I have created a custom user model 'User' and have declared in settings.py as AUTH_USER_MODEL
Also i have two other models consumer and workman which has an onetoone relation with my custom user.
i created two forms for registering customer and workman but as i fill form(it doesnt even check validation) and submit, it says: Manager isn't available; 'auth.User' has been swapped for 'accounts.User'
models.py:
from django.contrib.auth.models import AbstractUser
from django.contrib.auth import get_user_model
# Create your models here.
class User(AbstractUser):
class Types(models.TextChoices):
CONSUMER = "CONSUMER" , "Consumer"
WORKMAN = "WORKMAN" , "Workman"
type = models.CharField(max_length=20,choices=Types.choices,default=Types.WORKMAN)
is_consumer = models.BooleanField(default=False)
is_workman = models.BooleanField(default=False)
class Consumer(models.Model):
user = models.OneToOneField(get_user_model(),on_delete=models.CASCADE,primary_key=True)
location = models.CharField(max_length=100)
class Workman(models.Model):
user = models.OneToOneField(get_user_model(),on_delete=models.CASCADE,primary_key=True)
contact = models.CharField(max_length=100)
views.py:
from .forms import ConsumerCreationForm, WorkmanCreationForm
from .models import Workman,Consumer
class consumersignupview(CreateView):
model = Consumer
form_class = ConsumerCreationForm
template_name = 'accounts/register.html'
success_url = '/'
class workmansignupview(CreateView):
model = Workman
form_class = WorkmanCreationForm
template_name = 'accounts/register.html'
success_url = '/'
forms.py:
from django.contrib.auth.forms import UserCreationForm
from django import forms
from .models import Consumer,Workman
class ConsumerCreationForm(UserCreationForm):
location = forms.CharField( max_length=100, required=False)
class meta(UserCreationForm.Meta):
model = Consumer
class WorkmanCreationForm(UserCreationForm):
contact = forms.CharField(max_length=100)
class meta(UserCreationForm.Meta):
model = Workman
error:
full traceback:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/account/wregister/
Django Version: 3.2.9
Python Version: 3.8.2
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts.apps.AccountsConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "F:\Project Work\Proxy-Model\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "F:\Project Work\Proxy-Model\venv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "F:\Project Work\Proxy-Model\venv\lib\site-packages\django\views\generic\base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "F:\Project Work\Proxy-Model\venv\lib\site-packages\django\views\generic\base.py", line 98, in dispatch
return handler(request, *args, **kwargs)
File "F:\Project Work\Proxy-Model\venv\lib\site-packages\django\views\generic\edit.py", line 172, in post
return super().post(request, *args, **kwargs)
File "F:\Project Work\Proxy-Model\venv\lib\site-packages\django\views\generic\edit.py", line 141, in post
if form.is_valid():
File "F:\Project Work\Proxy-Model\venv\lib\site-packages\django\forms\forms.py", line 175, in is_valid
return self.is_bound and not self.errors
File "F:\Project Work\Proxy-Model\venv\lib\site-packages\django\forms\forms.py", line 170, in errors
self.full_clean()
File "F:\Project Work\Proxy-Model\venv\lib\site-packages\django\forms\forms.py", line 374, in full_clean
self._post_clean()
File "F:\Project Work\Proxy-Model\venv\lib\site-packages\django\contrib\auth\forms.py", line 117, in _post_clean
super()._post_clean()
File "F:\Project Work\Proxy-Model\venv\lib\site-packages\django\forms\models.py", line 413, in _post_clean
self.instance.full_clean(exclude=exclude, validate_unique=False)
File "F:\Project Work\Proxy-Model\venv\lib\site-packages\django\db\models\base.py", line 1223, in full_clean
self.clean()
File "F:\Project Work\Proxy-Model\venv\lib\site-packages\django\contrib\auth\models.py", line 371, in clean
self.email = self.__class__.objects.normalize_email(self.email)
File "F:\Project Work\Proxy-Model\venv\lib\site-packages\django\db\models\manager.py", line 187, in __get__
raise AttributeError(
Exception Type: AttributeError at /account/wregister/
Exception Value: Manager isn't available; 'auth.User' has been swapped for 'accounts.User'
This error is a repeat of this issue. You are likely importing the wrong user model somewhere.

Django REST framework - 3 models related to one another, DELETE request fails

I have an issue with deleting an instance of a model (let's call it A) which has an instance of another model (B) related to A by Foreign Key constraint, and fails to delete. It's using CASCADE parameter, however I get response 500 which in the tracebacks just says that the server has crashed/database went into recovery mode.
The following is my code:
views.py
class TaskInstance(generics.RetrieveUpdateDestroyAPIView):
"""
Returns Task instance
"""
queryset = Task.objects.all()
serializer_class = TaskSerializer
class StepList(generics.ListCreateAPIView):
"""
List all Steps (OR for specified task), or create a new one
"""
queryset = Step.objects.all()
serializer_class = StepSerializer
filter_fields = ('task',)
models.py
class Category(models.Model):
name = models.CharField(max_length=25, blank=False)
class Meta:
ordering = ('id',)
class Task(models.Model):
name = models.CharField(max_length=25, blank=False)
cat = models.ForeignKey(Category, related_name='tasks', on_delete=models.CASCADE)
class Meta:
ordering = ('id',)
class Step(models.Model):
name = models.CharField(max_length=25, blank=False)
completed = models.BooleanField(blank=True, default=False)
task = models.ForeignKey(Task, related_name='steps', on_delete=models.CASCADE)
class Meta:
ordering = ('id',)
serializers.py
class CategorySerializer(serializers.ModelSerializer):
tasks = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
class Meta:
model = Category
fields = ('id', 'name', 'tasks')
class TaskSerializer(serializers.ModelSerializer):
steps = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
class Meta:
model = Task
fields = ('id', 'name', 'steps')
class StepSerializer(serializers.ModelSerializer):
task = serializers.PrimaryKeyRelatedField(queryset=Task.objects.all(), many=False)
class Meta:
model = Step
fields = ('id', 'name', 'completed')
My perfect scenario: I'd like to be able to send a DELETE request for specific Task model, which deletes every Step model that relates to it.
I'd appreciate any help!
Traceback
OperationalError at /task/1/
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
Request Method: DELETE
Request URL: http://localhost:7000/task/1/
Django Version: 2.1.3
Python Executable: C:\Users\vaida\Documents\Coding\android-tm-api\venv\Scripts\python.exe
Python Version: 3.7.1
Python Path: ['C:\\Users\\vaida\\Documents\\Coding\\android-tm-api\\android_tm_api', 'C:\\Users\\vaida\\Documents\\Coding\\android-tm-api\\venv\\Scripts\\python37.zip', 'C:\\Users\\vaida\\Documents\\Coding\\android-tm-api\\venv\\DLLs', 'C:\\Users\\vaida\\Documents\\Coding\\android-tm-api\\venv\\lib', 'C:\\Users\\vaida\\Documents\\Coding\\android-tm-api\\venv\\Scripts', 'c:\\users\\vaida\\appdata\\local\\programs\\python\\python37\\Lib', 'c:\\users\\vaida\\appdata\\local\\programs\\python\\python37\\DLLs', 'C:\\Users\\vaida\\Documents\\Coding\\android-tm-api\\venv', 'C:\\Users\\vaida\\Documents\\Coding\\android-tm-api\\venv\\lib\\site-packages']
Server time: Fri, 16 Nov 2018 20:05:14 +0000
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'api.apps.ApiConfig',
'django_filters']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\django\db\backends\base\base.py" in _commit
239. return self.connection.commit()
The above exception (server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
) was the direct cause of the following exception:
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\django\db\transaction.py" in __exit__
212. connection.commit()
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\django\db\backends\base\base.py" in commit
261. self._commit()
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\django\db\backends\base\base.py" in _commit
239. return self.connection.commit()
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\django\db\utils.py" in __exit__
89. raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\django\db\backends\base\base.py" in _commit
239. return self.connection.commit()
During handling of the above exception (server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
), another exception occurred:
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\django\db\backends\base\base.py" in ensure_connection
216. self.connect()
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\django\db\backends\base\base.py" in connect
194. self.connection = self.get_new_connection(conn_params)
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\django\db\backends\postgresql\base.py" in get_new_connection
178. connection = Database.connect(**conn_params)
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\psycopg2\__init__.py" in connect
130. conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
The above exception (server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
) was the direct cause of the following exception:
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\django\core\handlers\base.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
54. return view_func(*args, **kwargs)
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\django\views\generic\base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\rest_framework\views.py" in dispatch
495. response = self.handle_exception(exc)
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\rest_framework\views.py" in handle_exception
455. self.raise_uncaught_exception(exc)
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\rest_framework\views.py" in dispatch
492. response = handler(request, *args, **kwargs)
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\rest_framework\generics.py" in delete
293. return self.destroy(request, *args, **kwargs)
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\rest_framework\mixins.py" in destroy
93. self.perform_destroy(instance)
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\rest_framework\mixins.py" in perform_destroy
97. instance.delete()
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\django\db\models\base.py" in delete
880. return collector.delete()
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\django\db\models\deletion.py" in delete
306. sender=model, instance=obj, using=self.using
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\django\db\transaction.py" in __exit__
256. connection.set_autocommit(True)
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\django\db\backends\base\base.py" in set_autocommit
394. self.ensure_connection()
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\django\db\backends\base\base.py" in ensure_connection
216. self.connect()
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\django\db\utils.py" in __exit__
89. raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\django\db\backends\base\base.py" in ensure_connection
216. self.connect()
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\django\db\backends\base\base.py" in connect
194. self.connection = self.get_new_connection(conn_params)
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\django\db\backends\postgresql\base.py" in get_new_connection
178. connection = Database.connect(**conn_params)
File "C:\Users\vaida\Documents\Coding\android-tm-api\venv\lib\site-packages\psycopg2\__init__.py" in connect
130. conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
Exception Type: OperationalError at /task/1/
Exception Value: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
After a while I figured out that I was approaching the problem the wrong way... It wasn't the code's fault - it works fine. The problem was to do with database set up through pgAdmin. Once dropped and Django project re-migrated, everything seems to work fine.

Django: how to use two different databases with Wagtail CMS

Inside a Django project, I have one app, otherapp, which hits a Postgres database on a remote server that contains scraped data. I have a second app, content, which hits a different Postgres database on the same remote server, and contains pages I'd like to have served through the Wagtail CMS.
I installed Wagtail locally using these instructions (I did not use the Wagtail installer). I got it working locally. Then, I did a pg_dump of the local database and did psql db2 < db2dumpfile.sql on the remote database server.
Each of the apps works fine locally in isolation, but I can't get them to work together. I thought I could use a database router to specify which database I want used to retrieve different types of data.
But, when I put the database router into the settings file, it starts to fail. How can I fix this? Do I need to declare wagtailcore somewhere else in the project?
settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'db1',
'USER': DB_USERNAME,
'PASSWORD': DB_PASSWORD,
'HOST': HOST,
'PORT': PORT
},
'CMS': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'db2',
'USER': DB_USERNAME,
'PASSWORD': DB_PASSWORD,
'HOST': HOST,
'PORT': PORT
}
}
DATABASE_ROUTERS = [ 'projectname.routers.FindRouter',]
routers.py:
import os
from django.conf import settings
import socket
class FindRouter(object):
def db_for_read(self, model, **hints) :
if model._meta.app_label == 'content' :
return 'CMS'
return None
def db_for_read(self, model, **hints):
if model._meta.app_label == 'content' :
return 'CMS'
return None
def db_for_write(self, model, **hints):
if model._meta.app_label == 'content' :
return 'CMS'
return None
def allow_relation(self, obj1, obj2, **hints):
if obj1._meta.app_label == 'content' or obj2._meta.app_label == 'content':
return True
return None
def allow_migrate(self, db, app_label, model=None, **hints):
if app_label == 'content' :
return db == 'CMS'
return None
This is the error I am getting when I do runserver:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/cms/
Django Version: 1.9
Python Version: 2.7.10
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'wagtail.wagtailforms',
'wagtail.wagtailredirects',
'wagtail.wagtailembeds',
'wagtail.wagtailsites',
'wagtail.wagtailusers',
'wagtail.wagtailsnippets',
'wagtail.wagtaildocs',
'wagtail.wagtailimages',
'wagtail.wagtailsearch',
'wagtail.wagtailadmin',
'wagtail.wagtailcore',
'modelcluster',
'compressor',
'taggit',
'otherapp',
'content']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
'wagtail.wagtailcore.middleware.SiteMiddleware',
'wagtail.wagtailredirects.middleware.RedirectMiddleware']
Traceback:
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
123. response = middleware_method(request)
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/wagtail/wagtailcore/middleware.py" in process_request
11. request.site = Site.find_for_request(request)
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/wagtail/wagtailcore/models.py" in find_for_request
122. return Site.objects.get(hostname=hostname) # Site.DoesNotExist here goes to the final except clause
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/models/manager.py" in manager_method
122. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/models/query.py" in get
381. num = len(clone)
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/models/query.py" in __len__
240. self._fetch_all()
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/models/query.py" in _fetch_all
1074. self._result_cache = list(self.iterator())
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/models/query.py" in __iter__
52. results = compiler.execute_sql()
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
852. cursor.execute(sql, params)
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
79. return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/utils.py" in __exit__
95. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
Exception Type: ProgrammingError at /cms/
Exception Value: relation "wagtailcore_site" does not exist
LINE 1: ...ge_id", "wagtailcore_site"."is_default_site" FROM "wagtailco...
^
I gave up and put the Wagtail tables back into the first database, and now the two parts of the application are working together fine.

Tastypie relationship fields for more than 2 models

I'm learning to write an API using tastypie for a Django project. I have following code in models.py:
from django.db import models
from django.contrib.auth.models import User
class UserDeviceIds(models.Model):
user = models.ForeignKey(User)
device_id = models.CharField(max_length=15)
def __str__(self):
return self.device_id
class UserProfile(models.Model):
user = models.OneToOneField(User)
referral_code = models.CharField(max_length=8, unique=True)
def __str__(self):
return str(self.mal_rank)
And, I have following code in api.py:
class CreateUserProfileResource(ModelResource):
user = fields.ForeignKey('users.api.CreateUserResource', 'user', full=True)
class Meta:
list_allowed_methods = ['get','post']
always_return_data = True
authorization = Authorization()
authentication = Authentication()
validation = CreateUserProfileValidation()
resource_name = 'auth'
queryset = UserProfile.objects.all()
def hydrate(self, bundle):
bundle.data["user"]['username'] = bundle.data.get('country_code') + bundle.data.get("user")['username']
# Set a password automatically:
raw_password = ''.join(random.choice(string.ascii_lowercase) for i in range(8))
u = User(username='dummy')
u.set_password(raw_password)
bundle.data["user"]['password'] = u.password
return bundle
class CreateUserResource(ModelResource):
class Meta:
authorization = Authorization()
authentication = Authentication()
always_return_data = True
resource_name = 'user'
queryset = User.objects.all()
excludes = ['is_active', 'is_staff', 'is_superuser', 'date_joined', 'last_login']
When I send a POST request to http://127.0.0.1:8000/api/v1/auth (i.e:
curl -X POST -H "Content-Type: application/json" -d '{"user": {"email":"a#b.com","username":"abcdef"}, "referral_code":"abc123"}' http://127.0.0.1:8000/api/v1/auth
then a User and a UserProfile object is successfully created. But I would also like to create a UserDeviceIds object in the same endpoint. I have tried combination of different Tastypie Relationship Fields but I can't create a UserDeviceIds object. Can someone please elaborate on Tastypie relationship fields with some sample code to make me understand how the relationships work in Django?
For instance, I edited my CreateUserProfileResource in api.py and added the following line:
deviceid = fields.ForeignKey('users.api.CreateUserDeviceIdsResource', 'deviceid', full=True)
so that CreateUserProfileResource now looks like:
class CreateUserProfileResource(ModelResource):
user = fields.ForeignKey('users.api.CreateUserResource', 'user', full=True)
deviceid = fields.ForeignKey('users.api.CreateUserDeviceIdsResource', 'deviceid', full=True)
class Meta:
list_allowed_methods = ['get','post']
...
...
and added a new Resource for UserDeviceIds model:
class CreateUserDeviceIdsResource(ModelResource):
class Meta:
authorization = Authorization()
authentication = Authentication()
always_return_data = True
resource_name = 'deviceid'
queryset = UserDeviceIds.objects.all()
and when I try to send a POST request:
curl -X POST -H "Content-Type: application/json" -d '{"user": {"email":"a#b.com","username":"abcdef"}, "referral_code":"abc123", "deviceid": {"device_id": "abc"}}' http://127.0.0.1:8000/api/v1/auth
I get following error traceback:
{"error_message": "null value in column \"user_id\" violates not-null constraint
DETAIL: Failing row contains (2, abc, def, ghi, null).
", "traceback": "Traceback (most recent call last):
File \"/vagrant/venv/lib/python3.4/site-packages/django/db/backends/utils.py\", line 65, in execute
return self.cursor.execute(sql, params)
psycopg2.IntegrityError: null value in column \"user_id\" violates not-null constraint
DETAIL: Failing row contains (2, abc, def, ghi, null).
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File \"/vagrant/venv/lib/python3.4/site-packages/tastypie/resources.py\", line 201, in wrapper
response = callback(request, *args, **kwargs)
File \"/vagrant/venv/lib/python3.4/site-packages/tastypie/resources.py\", line 432, in dispatch_list
return self.dispatch('list', request, **kwargs)
File \"/vagrant/venv/lib/python3.4/site-packages/tastypie/resources.py\", line 464, in dispatch
response = method(request, **kwargs)
File \"/vagrant/venv/lib/python3.4/site-packages/tastypie/resources.py\", line 1340, in post_list
updated_bundle = self.obj_create(bundle, **self.remove_api_resource_names(kwargs))
File \"/vagrant/venv/lib/python3.4/site-packages/tastypie/resources.py\", line 2104, in obj_create
return self.save(bundle)
File \"/vagrant/venv/lib/python3.4/site-packages/tastypie/resources.py\", line 2247, in save
self.save_related(bundle)
File \"/vagrant/venv/lib/python3.4/site-packages/tastypie/resources.py\", line 2318, in save_related
related_resource.save(related_bundle)
File \"/vagrant/venv/lib/python3.4/site-packages/tastypie/resources.py\", line 2250, in save
bundle.obj.save()
File \"/vagrant/venv/lib/python3.4/site-packages/django/db/models/base.py\", line 589, in save
force_update=force_update, update_fields=update_fields)
File \"/vagrant/venv/lib/python3.4/site-packages/django/db/models/base.py\", line 617, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File \"/vagrant/venv/lib/python3.4/site-packages/django/db/models/base.py\", line 698, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File \"/vagrant/venv/lib/python3.4/site-packages/django/db/models/base.py\", line 731, in _do_insert
using=using, raw=raw)
File \"/vagrant/venv/lib/python3.4/site-packages/django/db/models/manager.py\", line 92, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File \"/vagrant/venv/lib/python3.4/site-packages/django/db/models/query.py\", line 921, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File \"/vagrant/venv/lib/python3.4/site-packages/django/db/models/sql/compiler.py\", line 920, in execute_sql
cursor.execute(sql, params)
File \"/vagrant/venv/lib/python3.4/site-packages/django/db/backends/utils.py\", line 81, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File \"/vagrant/venv/lib/python3.4/site-packages/django/db/backends/utils.py\", line 65, in execute
return self.cursor.execute(sql, params)
File \"/vagrant/venv/lib/python3.4/site-packages/django/db/utils.py\", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File \"/vagrant/venv/lib/python3.4/site-packages/django/utils/six.py\", line 658, in reraise
raise value.with_traceback(tb)
File \"/vagrant/venv/lib/python3.4/site-packages/django/db/backends/utils.py\", line 65, in execute
return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: null value in column \"user_id\" violates not-null constraint
DETAIL: Failing row contains (2, abc, def, ghi, null).
"}
which I understand means that when it tried to save a UserDeviceIds object, it could not find data in bundle to put in the ForeignKey(User) field of UserDeviceIds model.
Can someone explain what needs to be done to make this code work?
Thanks!
You can't achieve it in current relation. The model resources are true reflection of models. For instance you could create this:
>>> UserProfile.objects.create(refferal_code='asdfasdf', user=User.objects.create_user(username='asdfasdf'))
or this:
>>> UserDeviceIds.objects.create(device_id='adfadf', user=User.objects.create_user(username='asdfasdfas'))
But not both in one instruction.
The REST API is great because it quite well feet Django ORM own interface.
So you can create object with children but not with other parents at the same time.
You have named your Resources with functional purpose creation that clearly suggest something is not right. Your Resource should be assign only to model.
In my own opinion REST API is great for working with, because it's clear, easy to understand for clients, easy rules, fast to build, easy to test. But doesn't feet really well sign-up with Django.
If you will have to break some RESTful rules don't try to redefine existing fully functioning Resources it can give a lot of pain. Instead Make some custom separated endpoint well described for this purpose.
I suggest to create custom view like endpoint handling json/xml whatever you support for sign-up. Hold it in separated app if you find better solution in future you will easily replace it. I suggest it because: 1. It's critical for you to create these 3 models in one call, it will break models if internet gets off. 2. Will be faster.
I doubt in existence of the ready apps solving the problem because sing-up is always bit unique for each website.
import json
from django.contrib.auth.models import User
from django.http import (HttpResponse, HttpResponseBadRequest,
HttpResponseNotAllowed)
from django.views.decorators.csrf import csrf_exempt
#csrf_exempt
def signup(request):
"""
Description:
POST faldksfalskdfjalksdf
Headers:
sdfasdf
Attributes:
sadljfkasdlfksad
Responses:
asdf
"""
if request.method != 'POST':
return HttpResponseNotAllowed(permitted_methods=('POST',))
try:
data = json.loads(request.body)
except (AttributeError, ValueError):
return HttpResponseBadRequest(
json.dumps({'error': 'JSON format invalid',
'success': False}),
content_type='application/json')
if data.get('username', None) and data.get('device_id', None) and data.get('country', None):
# add more validation.
user = User.objects.create_user(username=data['username'])
UserProfile.objects.create(refferal_code='asdfasdf', user=user)
UserDeviceIds.objects.create(device_id='adfadf', user=user)
return HttpResponse(json.dumps({'success': True}),
content_type='application/json')
else:
return HttpResponseBadRequest(
json.dumps({'error': 'username, device_id or country attributes missing.',
'success': False}),
content_type='application/json')

Categories