StringBuffer对象比较(StringBuffer object comparision)
我创建了两个StringBuffer,如下所示
StringBuffer buffer1 = new StringBuffer("Text"); StringBuffer buffer2 = new StringBuffer(buffer1);
如果比较那些StringBuffer和equals方法然后它返回false?
if (buffer1.equals(buffer2)) System.out.println("true"); else System.out.println("false");
equals()比较string的内容。所以,我不知道在这里返回false的原因是什么...
请指导我摆脱这个问题?
I have created two StringBuffer as follows
StringBuffer buffer1 = new StringBuffer("Text"); StringBuffer buffer2 = new StringBuffer(buffer1);
If Compare those StringBuffer with equals method then it returned false?
if (buffer1.equals(buffer2)) System.out.println("true"); else System.out.println("false");
equals() compares the content of the string.So, i dont know what is the reason for returning false here...
Please Guide me get out of this issue?
原文:https://stackoverflow.com/questions/10331148
最满意答案
经过大量搜索,我终于解决了我的问题。 因此,事实证明,如果您尝试对管理员中的M2M字段执行某些操作,则更改将由clear()自动清除。
所以,我不得不建立一个新的模型管理类:
class PostAdmin(admin.ModelAdmin): class Meta: model = Post def save_model(self, request, obj, form, change): print "from save_model" obj.save() def save_related(self, request, form, formsets, change): super(PostAdmin, self).save_related(request, form, formsets, change) obj = form.instance if obj.body: tag_list = [Tag.objects.get_or_create(name=word)[0] for word in obj.body.split() if word.startswith("#")] obj.tags.add(*tag_list) print("from save_related") obj.save() admin.site.register(Post, PostAdmin)
另外,我改变了我在制作标签列表的方式,因为@Prakhar从
create()
指向get_or_create()
。 注意 get_or_create()期间的索引[0],以获取实例而不是created
实例。signals.py:
@receiver(post_save, sender=Post) def create(sender, instance, **kwargs): if kwargs.get('created', True): tag_list = [Tag.objects.get_or_create(name=word)[0] for word in instance.body.split() if word.startswith("#")] print "from signals!: instance body: %s" % instance.body instance.tags.add(*tag_list) else: tag_list = [Tag.objects.get_or_create(name=word)[0] for word in instance.body.split() if word.startswith("#")] print "already signals!: instance body: %s" % instance.body instance.tags.add(*tag_list)
After searching a lot, I finally solved my problem. So, it turns out that if you try to do something to the M2M field in the admin, the change will get wiped out by the clear() automatically.
So, I had to make a new model admin class:
class PostAdmin(admin.ModelAdmin): class Meta: model = Post def save_model(self, request, obj, form, change): print "from save_model" obj.save() def save_related(self, request, form, formsets, change): super(PostAdmin, self).save_related(request, form, formsets, change) obj = form.instance if obj.body: tag_list = [Tag.objects.get_or_create(name=word)[0] for word in obj.body.split() if word.startswith("#")] obj.tags.add(*tag_list) print("from save_related") obj.save() admin.site.register(Post, PostAdmin)
Also I changed how I was making list of tags as @Prakhar pointed out from
create()
toget_or_create()
. Note the indexing [0] during get_or_create(), to get the instance and not thecreated
.signals.py:
@receiver(post_save, sender=Post) def create(sender, instance, **kwargs): if kwargs.get('created', True): tag_list = [Tag.objects.get_or_create(name=word)[0] for word in instance.body.split() if word.startswith("#")] print "from signals!: instance body: %s" % instance.body instance.tags.add(*tag_list) else: tag_list = [Tag.objects.get_or_create(name=word)[0] for word in instance.body.split() if word.startswith("#")] print "already signals!: instance body: %s" % instance.body instance.tags.add(*tag_list)
相关问答
更多-
django-reversion恢复admin以外的ManyToMany字段(django-reversion revert ManyToMany fields outside admin)[2023-02-04]
如果我理解正确,我认为你应该得到该版本的修订 ; 该版本包含对象的数据,该版本包含多个对象的版本。 看一下: some_version.revision.version_set.all() 具体来说,我认为你应该使用(未经测试): [v for V版本.objects.get_for_date(product,ondate).revision.version_set.all()如果version.content_type == ContentType.objects.get_for_model(Sku)] ... -
经过几天的调试,我发现了这个问题。 在我的代码中,我使用来自django-extensions库的UUIDField作为主键。 保存练习模型的新实例时,它能够生成,设置和保存主键。 但是,在保存设置了ManyToMany字段的新练习实例时,不会及时生成UUIDField。 这导致Django管理员尝试将一个空/空主键(练习模型中的UUIDField)插入到连接表中,该表触发外键约束失败。 After several days of debugging I found the issue. In my cod ...
-
如何在Django中立即向ManyToMany关系添加多个对象?(How to add multiple objects to ManyToMany relationship at once in Django ?)[2022-09-02]
使用: object.m2mfield.add(*items) ,如文档中所述: add()接受任意数量的参数,而不是它们的列表。 add(obj1, obj2, obj3, ...) 要将该列表扩展为参数,请使用* add(*[obj1, obj2, obj3]) 附录: Django不会调用每个项目的bulk_create() ,而是使用bulk_create() 。 Use: object.m2mfield.add(*items) as described in the documentation ... -
django admin formfield for manytomany not working(django admin formfield for manytomany not working)[2023-05-11]
发布的代码不会抛出此错误。 如果您使用双下划线car__like它只会混淆连接。 您应该查看django查询参考以获取确切的详细信息。 无论如何, 也许你正在寻找生成LIKE语句的__icontains ? Foo.objects.filter(some_field__icontains='ford') The code as posted wouldn't throw this error. It can only confuse a join if you used double underscore ... -
django admin不会将对象添加到manytomany字段中(django admin does not add objects into manytomany fields)[2022-06-12]
经过大量搜索,我终于解决了我的问题。 因此,事实证明,如果您尝试对管理员中的M2M字段执行某些操作,则更改将由clear()自动清除。 所以,我不得不建立一个新的模型管理类: class PostAdmin(admin.ModelAdmin): class Meta: model = Post def save_model(self, request, obj, form, change): print "from save_model" o ... -
问题与此说明有关: post.categories = post.categorytopost_set 从Django 文档 : 与普通的多对多字段不同,您不能使用添加,创建或赋值(即beatles.members = [...])来创建关系。 在您的场景中,您应手动创建CategoryToPost对象,同时引用Post和Category并保存它。 The problem is related to this instruction : post.categories = post.categoryt ...
-
Django Admin Forms与ManyToMany关系中的其他字段(Django Admin Forms with additional fields in ManyToMany relations)[2021-11-01]
我明白了,我必须使用内联字段! Doc: https : //docs.djangoproject.com/en/dev/ref/contrib/admin/#working-with-many-to-many-intermediary-models I got it, I have to use Inline Fields! Doc: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#working-with-many-to-many-int ... -
直通表由标准的ForeignKeys链接,因此您可以进行正常的ForeignKey查找。 不要忘记每个自行车用户都有一个评论,即每个自行车/用户配对一个评论。 for myBikeUser in myBike.bikeuser_set.all(): print myBikeUser.comment, myBikeUser.user.first_name The through table is linked by standard ForeignKeys, so you do a normal F ...
-
class ReplyAdmin(admin.ModelAdmin): list_display = ["__str__", "user", "question", "creationDate", "get_pages",] def get_pages(self, obj): return "\n".join([page.title for page in obj.question.page.all()]) class Meta: model = ...
-
info = Batch.objects.create(season_title ='foo', .....) info2= AssetMetadata.objects.create(material_id ='some') info.material_id.add(info2) 尝试这样的事情 info = Batch.objects.create(season_title ='foo', .....) info2= AssetMetadata.objects.create(material_id =' ...