Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: User default language based on browser language #2201

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: User default language based on browser language
  • Loading branch information
shaohuzhang1 committed Feb 10, 2025
commit df089464ca26c68125549cc6cbb6ac22c1867a21
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.18 on 2025-02-10 06:01

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('application', '0025_alter_application_prologue'),
]

operations = [
migrations.AlterField(
model_name='applicationaccesstoken',
name='language',
field=models.CharField(default=None, max_length=10, null=True, verbose_name='语言'),
),
]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your migration looks generally correct, but there are a few things you might consider:

  1. Default Value for null: The default value of None can be ambiguous because in Python, many types cannot be determined from just None. If you want to explicitly indicate that null is allowed, it's better to use models.TextField(null=True) instead.

  2. Verbose Name: In Django, you typically only need to specify the verbose name if you're not using the default translation system, which is common these days. You could remove it entirely:

    field=models.CharField(max_length=10, null=True),
  3. Consistent Field Definition: It would also make sense to have all fields defined consistently (e.g., whether they're required or optional). For example, blank=True could be added for any non-required string fields:

    field=models.CharField(max_length=10, blank=True, null=True),

Here's an optimized version based on these suggestions:

@@ -0,0 +1,18 @@
+# Generated by Django 4.2.18 on 2025-02-10 06:01

+from django.db import migrations, models


+class Migration(migrations.Migration):

+    dependencies = [
+        ('application', '0025_alter_application_prologue'),
+    ]

+    operations = [
+        migrations.AlterField(
+            model_name='applicationaccesstoken',
+            name='language',
+            field=models.CharField(max_length=10, blank=True, null=True),
+        ),
+    ]

This version simplifies the model definition while maintaining clarity.

2 changes: 1 addition & 1 deletion apps/application/models/api_key_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class ApplicationAccessToken(AppModelMixin):
, default=list)
show_source = models.BooleanField(default=False, verbose_name="是否显示知识来源")

language = models.CharField(max_length=10, verbose_name="语言", default=get_language)
language = models.CharField(max_length=10, verbose_name="语言", default=None, null=True)

class Meta:
db_table = "application_access_token"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code has some minor adjustments to improve clarity and functionality:

  1. Null Check: The language field is now set to accept null=True, which allows for the possibility that it could be left empty.
    language = models.CharField(max_length=10, verbose_name="语言", default=None, null=True)
  1. Default Value Removal: Removed the call to get_language in the default value of the language field since it no longer returns an object (it used to return either 'zh_CN' or 'en_US'). Instead, simply set it to None.

  2. Minor Typographical Correction:

    • Changed verbose_name("是否显示知识来源") to verbose_name='是否显示知识来源'.

This should make the function more robust and handle edge cases like when the default option needs to be None.

Expand Down
18 changes: 18 additions & 0 deletions apps/users/migrations/0006_alter_user_language.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.18 on 2025-02-10 05:58

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('users', '0005_user_language'),
]

operations = [
migrations.AlterField(
model_name='user',
name='language',
field=models.CharField(default=None, max_length=10, null=True, verbose_name='语言'),
),
]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found in your migration script. The structure is correct and follows best practices for altering fields in Django database migrations.

2 changes: 1 addition & 1 deletion apps/users/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class User(AppModelMixin):
role = models.CharField(max_length=150, verbose_name="角色")
source = models.CharField(max_length=10, verbose_name="来源", default="LOCAL")
is_active = models.BooleanField(default=True)
language = models.CharField(max_length=10, verbose_name="语言", default=get_language)
language = models.CharField(max_length=10, verbose_name="语言", null=True, default=None)
create_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True, null=True)
update_time = models.DateTimeField(verbose_name="修改时间", auto_now=True, null=True)

Expand Down