How does Django maintain consistency between its logical constraints, and its database?
up vote
2
down vote
favorite
Lets say I have a model defined as follows:
class Foo(models.Model):
bar = models.CharField(max_length=10)
Using this model, I create a Foo
object in my database using the following code:
Foo.objects.create(bar="0123456789")
Should work well enough. However, after creating the object, I reduce the max_length
property to 5. So now, technically, the object I created has a consistency error.
How does Django manage a situation like this? I assume they don't raise any errors until that object is saved again. If so, is that the optimal solution for a situation like this, and are there are any other approaches?
django database django-database
add a comment |
up vote
2
down vote
favorite
Lets say I have a model defined as follows:
class Foo(models.Model):
bar = models.CharField(max_length=10)
Using this model, I create a Foo
object in my database using the following code:
Foo.objects.create(bar="0123456789")
Should work well enough. However, after creating the object, I reduce the max_length
property to 5. So now, technically, the object I created has a consistency error.
How does Django manage a situation like this? I assume they don't raise any errors until that object is saved again. If so, is that the optimal solution for a situation like this, and are there are any other approaches?
django database django-database
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Lets say I have a model defined as follows:
class Foo(models.Model):
bar = models.CharField(max_length=10)
Using this model, I create a Foo
object in my database using the following code:
Foo.objects.create(bar="0123456789")
Should work well enough. However, after creating the object, I reduce the max_length
property to 5. So now, technically, the object I created has a consistency error.
How does Django manage a situation like this? I assume they don't raise any errors until that object is saved again. If so, is that the optimal solution for a situation like this, and are there are any other approaches?
django database django-database
Lets say I have a model defined as follows:
class Foo(models.Model):
bar = models.CharField(max_length=10)
Using this model, I create a Foo
object in my database using the following code:
Foo.objects.create(bar="0123456789")
Should work well enough. However, after creating the object, I reduce the max_length
property to 5. So now, technically, the object I created has a consistency error.
How does Django manage a situation like this? I assume they don't raise any errors until that object is saved again. If so, is that the optimal solution for a situation like this, and are there are any other approaches?
django database django-database
django database django-database
asked Nov 10 at 17:39
darkhorse
1,34941844
1,34941844
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
It depends on the specific field property. In the case of max_length
, it's used in several validation steps in addition to the database itself.
The default html widget will insert a maxlength
attribute in the <input>
form element. And on the server side, Form.clean()
will also check the length of any char fields.
Finally, the database will throw an error (if you have created and applied a new migration after changing the field). But the specific behaviour here depends on the database backend.
Client side validation is just a usability feature. All data coming from the client must also be validated on the server, since client side validation can be bypassed by an attacker, or be turned off or unavailable in some clients.
Django's validation is what you use most of the time. It has error handling and http responses built in if you use a framework/app such as django forms, django admin or django rest framework.
The database validation is fallback when you insert some invalid data even if it's "not supposed to be possible". Errors here could result in a uncaught exception and the entire transaction will be rolled back. It's a very useful safety net when you have bugs in your django application.
add a comment |
up vote
3
down vote
Django won't do anything in the situation you describe. However, if you run makemigrations
, it will generate a database migration which modifies the db column to 5 characters; running this will truncate the existing values.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
It depends on the specific field property. In the case of max_length
, it's used in several validation steps in addition to the database itself.
The default html widget will insert a maxlength
attribute in the <input>
form element. And on the server side, Form.clean()
will also check the length of any char fields.
Finally, the database will throw an error (if you have created and applied a new migration after changing the field). But the specific behaviour here depends on the database backend.
Client side validation is just a usability feature. All data coming from the client must also be validated on the server, since client side validation can be bypassed by an attacker, or be turned off or unavailable in some clients.
Django's validation is what you use most of the time. It has error handling and http responses built in if you use a framework/app such as django forms, django admin or django rest framework.
The database validation is fallback when you insert some invalid data even if it's "not supposed to be possible". Errors here could result in a uncaught exception and the entire transaction will be rolled back. It's a very useful safety net when you have bugs in your django application.
add a comment |
up vote
1
down vote
accepted
It depends on the specific field property. In the case of max_length
, it's used in several validation steps in addition to the database itself.
The default html widget will insert a maxlength
attribute in the <input>
form element. And on the server side, Form.clean()
will also check the length of any char fields.
Finally, the database will throw an error (if you have created and applied a new migration after changing the field). But the specific behaviour here depends on the database backend.
Client side validation is just a usability feature. All data coming from the client must also be validated on the server, since client side validation can be bypassed by an attacker, or be turned off or unavailable in some clients.
Django's validation is what you use most of the time. It has error handling and http responses built in if you use a framework/app such as django forms, django admin or django rest framework.
The database validation is fallback when you insert some invalid data even if it's "not supposed to be possible". Errors here could result in a uncaught exception and the entire transaction will be rolled back. It's a very useful safety net when you have bugs in your django application.
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
It depends on the specific field property. In the case of max_length
, it's used in several validation steps in addition to the database itself.
The default html widget will insert a maxlength
attribute in the <input>
form element. And on the server side, Form.clean()
will also check the length of any char fields.
Finally, the database will throw an error (if you have created and applied a new migration after changing the field). But the specific behaviour here depends on the database backend.
Client side validation is just a usability feature. All data coming from the client must also be validated on the server, since client side validation can be bypassed by an attacker, or be turned off or unavailable in some clients.
Django's validation is what you use most of the time. It has error handling and http responses built in if you use a framework/app such as django forms, django admin or django rest framework.
The database validation is fallback when you insert some invalid data even if it's "not supposed to be possible". Errors here could result in a uncaught exception and the entire transaction will be rolled back. It's a very useful safety net when you have bugs in your django application.
It depends on the specific field property. In the case of max_length
, it's used in several validation steps in addition to the database itself.
The default html widget will insert a maxlength
attribute in the <input>
form element. And on the server side, Form.clean()
will also check the length of any char fields.
Finally, the database will throw an error (if you have created and applied a new migration after changing the field). But the specific behaviour here depends on the database backend.
Client side validation is just a usability feature. All data coming from the client must also be validated on the server, since client side validation can be bypassed by an attacker, or be turned off or unavailable in some clients.
Django's validation is what you use most of the time. It has error handling and http responses built in if you use a framework/app such as django forms, django admin or django rest framework.
The database validation is fallback when you insert some invalid data even if it's "not supposed to be possible". Errors here could result in a uncaught exception and the entire transaction will be rolled back. It's a very useful safety net when you have bugs in your django application.
edited Nov 10 at 20:07
answered Nov 10 at 18:06
Håken Lid
10.5k62441
10.5k62441
add a comment |
add a comment |
up vote
3
down vote
Django won't do anything in the situation you describe. However, if you run makemigrations
, it will generate a database migration which modifies the db column to 5 characters; running this will truncate the existing values.
add a comment |
up vote
3
down vote
Django won't do anything in the situation you describe. However, if you run makemigrations
, it will generate a database migration which modifies the db column to 5 characters; running this will truncate the existing values.
add a comment |
up vote
3
down vote
up vote
3
down vote
Django won't do anything in the situation you describe. However, if you run makemigrations
, it will generate a database migration which modifies the db column to 5 characters; running this will truncate the existing values.
Django won't do anything in the situation you describe. However, if you run makemigrations
, it will generate a database migration which modifies the db column to 5 characters; running this will truncate the existing values.
answered Nov 10 at 18:06
Daniel Roseman
442k41573628
442k41573628
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241677%2fhow-does-django-maintain-consistency-between-its-logical-constraints-and-its-da%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown