用表单Django更新自定义用户模型(Update custom user model with a form Django)
我有一个自定义的用户模型 ,我正在尝试创建一个表单,在提交时更新它。 我已经看过其他答案,并已尝试过他们的解决方案,即使我的代码几乎相同,它也无法正常工作。 当我运行这个时,我没有得到任何错误 。 表单不应创建新的UserAddress,但应更新其字段。
models.py
class UserAddress(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='user_address') your_address = models.CharField(max_length=300, blank=True, default=' ') #used in sign up @receiver(post_save, sender=User) def create_user_address(sender, instance, created, **kwargs): if created: UserAddress.objects.create(user=instance) @receiver(post_save, sender=User) def save_user_address(sender, instance, **kwargs): instance.user_address.save()
forms.py
class YourAddressForm(forms.ModelForm): class Meta: model = UserAddress fields = ('your_address', )
views.py
@login_required() def settings(request): if request.method == 'POST': form = YourAddressForm(request.POST, instance=request.user) if form.is_valid(): form.save() return HttpResponse('Awesome, I added it!') else: return HttpResponse("Not added") else: form = YourAddressForm(instance=request.user) return render(request, 'details/settings.html', {'form': form})
I have a custom user model I made, I am trying to make a form that will update it when submitted. I have looked at other answers on SO and have tried their solutions, even if my code is almost identical it will not work properly. I am getting no errors at all when I run this. The form shouldn't create a new UserAddress, but it should update its field.
models.py
class UserAddress(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='user_address') your_address = models.CharField(max_length=300, blank=True, default=' ') #used in sign up @receiver(post_save, sender=User) def create_user_address(sender, instance, created, **kwargs): if created: UserAddress.objects.create(user=instance) @receiver(post_save, sender=User) def save_user_address(sender, instance, **kwargs): instance.user_address.save()
forms.py
class YourAddressForm(forms.ModelForm): class Meta: model = UserAddress fields = ('your_address', )
views.py
@login_required() def settings(request): if request.method == 'POST': form = YourAddressForm(request.POST, instance=request.user) if form.is_valid(): form.save() return HttpResponse('Awesome, I added it!') else: return HttpResponse("Not added") else: form = YourAddressForm(instance=request.user) return render(request, 'details/settings.html', {'form': form})
原文:https://stackoverflow.com/questions/50310083
最满意答案
如果未指定别名,则会解决“sortorder”方面的第一个连接数据容器。 给定错误消息的类型,这可能是SQL Server数据库。
假设您正在寻找在特定数据容器中指定分区的方法,最好的方法是使用:
use select code partitioncode, 'eol' datacontaineralias from ...
这与取得以下结果相同:
select 'use ' || lst from ( select listagg(code || '@' || eol') lst from ... )
并执行那个。
当然,如果有多个数据容器连接,如Exact Online Belgium,荷兰和Loket和Nmbrs,您可以在一个
use
声明中提供它们。When the alias is not specified, the first connected data container in terms of "sortingorder" is addressed. Given the type of error message, that is probably a SQL Server database.
Assuming that you are looking for a way to specify the partitions in a specific data container, the best approach is to use:
use select code partitioncode, 'eol' datacontaineralias from ...
This is identical to taking the outcome of:
select 'use ' || lst from ( select listagg(code || '@' || eol') lst from ... )
and executing that one.
Of course, when you have multiple data containers connected like Exact Online Belgium, the Netherlands and Loket and Nmbrs, you can provide them all in one
use
statement.
相关问答
更多-
SELECT PARENT_ID FROM rel GROUP BY PARENT_ID HAVING SUM(PROP_ID NOT IN (5,1)) = 0 AND SUM(PROP_ID = 1) = 1 AND SUM(PROP_ID = 5) = 1 SELECT PARENT_ID FROM rel GROUP BY PARENT_ID HAVING SUM(PROP_ID NOT IN (5,1)) = 0 AND SUM(PROP_ID = 1) = 1 AND SUM(PROP_I ...
-
从一个角度看SSAS 2005事实,查询的多个分区好吗?(SSAS 2005 fact from a view, multiple partitions from queries ok?)[2022-05-14]
是的,它正如您所描述的那样。 Yes, it is exactly as you described. -
销售交易的许多Exact Online excel公式的表现(Performance of many Exact Online excel formulas for sales transactions)[2023-08-24]
通过在模型级别使用触发器,对于指示的数据量,运行时间减少到几分钟。 此触发器触发使用Exact Online REST API检索总计事务处理信息的总计数量的SQL语句: create or replace table itmrev@inmemorystorage as select itemcode , mnd , sum(qty) qty from ( select itemcode , year(date) * 12 + month(date) m ... -
HIVE“show partitions”命令不显示正确的分区(HIVE “show partitions” command do not show correct partitions)[2023-08-08]
Truncate删除表的数据文件。 它不会从Metastore中删除分区定义。 它不会删除文件系统目录。 演示 hive> create table mytable (i int) partitioned by (p int); OK hive> insert into mytable partition (p) values (1,10),(2,10),(3,20),(4,30),(5,30),(6,30); OK hive> select * from mytable; OK mytable.i ... -
如果未指定别名,则会解决“sortorder”方面的第一个连接数据容器。 给定错误消息的类型,这可能是SQL Server数据库。 假设您正在寻找在特定数据容器中指定分区的方法,最好的方法是使用: use select code partitioncode, 'eol' datacontaineralias from ... 这与取得以下结果相同: select 'use ' || lst from ( select listagg(code || '@' || eol') lst from ... ) ...
-
使用Invantive在Exact Online上手工制作的OData查询(Handcrafted OData queries on Exact Online with Invantive)[2020-10-29]
您可以使用Data Replicator或通过本机平台请求发送手工OData查询,例如: insert into NativePlatformScalarRequests ( url , orig_system_group ) select replace('https://start.exactonline.nl/api/v1/{division}/financial/Journals?$select=BankAccountIBAN,BankAccountDescription&$orderby=Ban ... -
按列表和范围同时分区 - 多分区MySQL(Partition By List and Range at the same time - Multiple Partitions MySQL)[2024-04-12]
我发现自己解决这个问题的方法是“复合分区”或者也称为“子分区” 这里是细节 其他DBMS也支持子分区 I found answer myself the solution to this is "Composite Partitioning" or also known as "Sub-Partitioning" Here are details Sub-Partitioning is also supported by other DBMS too -
您可以使用以下语法将一个语句上传到多个公司/部门,如UploadXMLTopics所述 : insert into UploadXMLTopics@eol ( topic , payload , division_code ) select topic , filecontents , division_code from ... 即使一个或多个上载失败,插入也不会报告错误并处理所有XML消息。 您可以使用以下方式查看结果: select * from UploadXMLTop ...
-
itgenobr001:找不到客户端。(itgenobr001: Client not found. on Data Access Point with Exact Online Belgium)[2021-09-07]
实际上解决起来非常简单:提到的“客户端”是应用程序。 我需要将Exact Online应用程序的客户端ID添加到我的连接字符串,因为数据访问点在使用生产许可证时需要客户端ID。 产生的数据容器规范:显示的use语句适用于只有一个数据容器的数据库。 在这种情况下,只有一个数据容器可以处理问题,一切运行顺利。 使用Invantive SQL中的分布式查询,您需要指定要使用哪个数据容器的use语句。 否则,第一个数据容器将尝试处理它(在这种情况下可能是没有分区概念的Freshdesk)。 这类似于将数据容器别名附加到每个表,如下所示: select ... from table@eolnl join table2@freshdesk on ... 这里eolnl和freshdesk指定了表 ...相关文章
更多- 在django中加入搜索引擎
- vicalloy的我与django
- Solr: a custom Search RequestHandler
- django-haystack+solr实现搜索
- Django and full-text search
- My W3C Custom Mapping File
- Django资源汇总(转)
- Securing Solr on Tomcat access using a user account
- Django下载及安装
- Haystack - Search for Django
最新问答
更多- 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
- 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
- OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
- 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
- codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
- 在计算机拍照在哪里进入
- 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
- No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
- 单页应用程序:页面重新加载(Single Page Application: page reload)
- 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
- System.StackOverflow错误(System.StackOverflow error)
- KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
- 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
- android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
- TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
- 企业安全培训的各项内容
- 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
- C#类名中允许哪些字符?(What characters are allowed in C# class name?)
- NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
- 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
- 将多个行和可变行移动到列(moving multiple and variable rows to columns)
- 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
- 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
- Angular $资源不会改变方法(Angular $resource doesn't change method)
- 在Angular 5中不是一个函数(is not a function in Angular 5)
- 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
- 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
- 常见的python rpc和cli接口(Common python rpc and cli interface)
- Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
- 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)