Re: Matching query does not exist after a post_data signal, but in fact does exist when visiting the url sent by the query

2017-05-17 Thread Constantine Covtushenko
Hi, François-Xavier

I believe that the reason of that is a transaction that is not committed
yet  when your code reaches 'post_save' listener.
And when you use 'requests' library there is nothing in the DB yet.

And I am just curios why there is a need of saving 'admin view' in the DB?

Regards,
Constantine C.

On Tue, May 16, 2017 at 10:17 PM, François-Xavier Cao 
wrote:

> Hi everone,
>
>
> I started to code a two-apps django project. ModelA belongs to appone and
> ModelB belongs to apptwo. My purpose is to create a ModelA instance
> everytime that the user creates a ModelB instance. And the value of a
> ModelA CharField (that is ckeditor widgeted) must be the source code of a
> ModelB admin view. I used a post_data signal to link a function of creation
> for that. The problem is that i use the id of each instance of ModelB in
> order to create the good content for each instance of ModelA. When I try to
> use a string of the url sending the id parameter, the content field has for
> value the source code of the debug page
>
> (error 500, DoesNotExist at /admin/apptwo/modelb/my_view/ref=76, [76 is
> an example] ModelB matching query does not exist. Exception location :
> /home/me/Desktop/env/lib/python3.5/site-packages/django/db/models/query.py
> in get, line 385)
>
> But when I try to visit the url "http://localhost:8000//admin/
> apptwo/modelb/my_view/ref=76", or when I hardcode the url, without a str(
> instance.id), the page exists and everything works perfectly.
>
> I don't understand why.
>
> Could anybody give me some help to solve this problem ?
>
> Thanks in advance,
>
> --
>
> PS :
>
> The first app has a model.py that contains the following code :
>
>
> from django.db import models
> from django.contrib.auth.models import Userfrom registre.models import *
> class ModelA(models.Model):
> content = models.CharField(max_length=255, null=True)
> def __str__(self):
> return "ModelA : " + str(self.id)
>
>
>
>
> the admin.py of this first app also contains :
>
>
> from django.contrib import admin
> from appone.models import *
> from apptwo.models import ModelB
> from django.http import HttpResponse
> from django.template.response import TemplateResponse
> from django.conf.urls import url
> from registre import views
> from django.db.models.signals import post_save
> from django.dispatch import receiver
> import datetime
> from django.contrib.auth.models import User
> from django import forms
> from ckeditor.widgets import CKEditorWidget
> from django.template.loader import render_to_string
> import requests
>
> class ModelAAdminForm(forms.ModelForm):
> content = forms.CharField(widget=CKEditorWidget())
> class Meta:
> model = ModelA
> fields = '__all__'
>
> class ModelAAdmin(admin.ModelAdmin):
> form = ModelAAdminForm
>
> def create_A(sender, instance, **kwargs):
> string = "http://localhost:8000/admin/apptwo/modelb/my_view/ref=; + 
> str(instance.id)
> r = requests.get(string)
> ModelA.objects.create(contenu=r.text.encode('utf-8'))
>
> post_save.connect(create_A, sender=ModelB)
>
> admin.site.register(ModelA, ModelAAdmin)
>
>
>
>
>
> the second app (apptwo) has a models.py like this :
>
>
>
> from django.db import models
> from django.contrib.auth.models import User
>
> class ModelB(models.Model):
> owner = models.ForeignKey(User, null=True)
> name = models.CharField(max_length=255, null=True)
>
> def __str__(self):
> return self.name
>
>
>
>
> and an admin.py that contains :
>
>
>
> from django.contrib import admin
> from appone.models import *
> from apptwo.models import *
> import datetime
> from django.conf.urls import url, include
> from django.template.response import TemplateResponse
>
> class ModelBAdmin(admin.ModelAdmin):
>
> def get_queryset(self, request):
> qs = super(ModelB, self).get_queryset(request)
> if request.user.is_superuser:
> return qs
> return qs.filter(owner=request.user)
>
> def save_model(self, request, obj, form, change):
> obj.owner = request.user
> obj.save()
>
> def get_urls(self):
> urls = super(ModelBAdmin, self).get_urls()
> my_urls = [
> url(r'^my_view/ref=(?P\d+)$', self.my_view),
> ]
> return my_urls + urls
>
> def my_view(self, request, id):
> context = dict(
>self.admin_site.each_context(request),
>selector = ModelB.objects.get(id=id),
> )
> return TemplateResponse(request, "myview.html", context)
>
> admin.site.register(ModelB, ModelBAdmin)
>
>
>
>
>
>
>
> and finally a template myview.html with :
>
>
> Test {{ selector.name }}
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop 

Matching query does not exist after a post_data signal, but in fact does exist when visiting the url sent by the query

2017-05-16 Thread François-Xavier Cao
 

Hi everone, 


I started to code a two-apps django project. ModelA belongs to appone and 
ModelB belongs to apptwo. My purpose is to create a ModelA instance 
everytime that the user creates a ModelB instance. And the value of a 
ModelA CharField (that is ckeditor widgeted) must be the source code of a 
ModelB admin view. I used a post_data signal to link a function of creation 
for that. The problem is that i use the id of each instance of ModelB in 
order to create the good content for each instance of ModelA. When I try to 
use a string of the url sending the id parameter, the content field has for 
value the source code of the debug page 

(error 500, DoesNotExist at /admin/apptwo/modelb/my_view/ref=76, [76 is an 
example] ModelB matching query does not exist. Exception location : 
/home/me/Desktop/env/lib/python3.5/site-packages/django/db/models/query.py 
in get, line 385)

But when I try to visit the url "
http://localhost:8000//admin/apptwo/modelb/my_view/ref=76;, or when I 
hardcode the url, without a str(instance.id), the page exists and 
everything works perfectly.

I don't understand why. 

Could anybody give me some help to solve this problem ?

Thanks in advance,

--

PS : 

The first app has a model.py that contains the following code : 


from django.db import models
from django.contrib.auth.models import Userfrom registre.models import *
class ModelA(models.Model):
content = models.CharField(max_length=255, null=True)
def __str__(self):
return "ModelA : " + str(self.id)




the admin.py of this first app also contains : 


from django.contrib import admin
from appone.models import *
from apptwo.models import ModelB
from django.http import HttpResponse
from django.template.response import TemplateResponse
from django.conf.urls import url
from registre import views
from django.db.models.signals import post_save
from django.dispatch import receiver
import datetime
from django.contrib.auth.models import User
from django import forms
from ckeditor.widgets import CKEditorWidget
from django.template.loader import render_to_string
import requests

class ModelAAdminForm(forms.ModelForm):
content = forms.CharField(widget=CKEditorWidget())
class Meta:
model = ModelA
fields = '__all__'

class ModelAAdmin(admin.ModelAdmin):
form = ModelAAdminForm

def create_A(sender, instance, **kwargs):
string = "http://localhost:8000/admin/apptwo/modelb/my_view/ref=; + 
str(instance.id)
r = requests.get(string)
ModelA.objects.create(contenu=r.text.encode('utf-8'))

post_save.connect(create_A, sender=ModelB) 

admin.site.register(ModelA, ModelAAdmin)





the second app (apptwo) has a models.py like this : 



from django.db import models
from django.contrib.auth.models import User

class ModelB(models.Model):
owner = models.ForeignKey(User, null=True)
name = models.CharField(max_length=255, null=True)

def __str__(self):
return self.name




and an admin.py that contains : 



from django.contrib import admin
from appone.models import *
from apptwo.models import *
import datetime
from django.conf.urls import url, include
from django.template.response import TemplateResponse

class ModelBAdmin(admin.ModelAdmin):

def get_queryset(self, request):
qs = super(ModelB, self).get_queryset(request)
if request.user.is_superuser:
return qs
return qs.filter(owner=request.user)

def save_model(self, request, obj, form, change):
obj.owner = request.user
obj.save()

def get_urls(self):
urls = super(ModelBAdmin, self).get_urls()
my_urls = [
url(r'^my_view/ref=(?P\d+)$', self.my_view),
]
return my_urls + urls

def my_view(self, request, id):
context = dict(
   self.admin_site.each_context(request),
   selector = ModelB.objects.get(id=id),
)
return TemplateResponse(request, "myview.html", context)

admin.site.register(ModelB, ModelBAdmin)







and finally a template myview.html with : 


Test {{ selector.name }}

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c146a55b-12ed-47d8-8bcb-7b5fc405b739%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.