Monday, April 30, 2018

I am following the official Django documentation and trying to make the polls app. But when I run the command python manage.py makemigrations polls I get the console error.

The code I am using for the models is:

from django.db import models      class Question(models.Model):      question_text = models.CharField(max_length=200)      pub_date = models.DateTimeField('date published')      class Choice(models.Model):     question = models.ForeignKey(Question)     choice_text = models.CharField(max_length=200)     votes = models.IntegerField(default=0)
The code i am using for mysite/settings is:
INSTALLED_APPS = [      'polls.apps.PollsConfig',      'django.contrib.admin',      'django.contrib.auth',      'django.contrib.contenttypes',      'django.contrib.sessions',      'django.contrib.messages',      'django.contrib.staticfiles',  ]

It would be great if you could point out the error.

--
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+unsubscribe@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/434e4e83-13f5-4688-b388-6c4d767a5273%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: Using Django Channels to subscribe to a Redis channel

Did you have luck with this. I've tried the same thing, but it seems like if no one is consuming on the other side I get a "channels.exceptions.ChannelFull" exception. 

I'm trying to provide a real-time feed for an exchange. There might be zero people listing or 1 million listening. The high level design should be the same. 



On Thursday, February 23, 2017 at 7:52:11 AM UTC-7, Jochen Breuer wrote:
Hi Andrew,

thanks for your very detailed answer! This is much simpler that anticipated. You are absolutely right, a management command is the most simple approach here. That was the missing piece in my picture. Thank you very much!

Jochen






Am Mittwoch, 22. Februar 2017 19:55:33 UTC+1 schrieb Andrew Godwin:
Hi Jochen,

Your problem is that if you want to listen to the pubsub channel you will need a dedicated process to do so, as you can't just poll something like that. Given that restriction, you're going to have to write something like a management command that opens a connection to Redis and listens for messages, and then whenever it gets one, sends the message onto a channel in the Django channel system.

You can send to channels from anywhere, so it's just a basic listener with one line of channel send added. You're likely to want some sort of logic on these messages, I suspect, so I would send onto a single custom channel and then, now your messages are in the evented channel system, you can then write a consumer for that channel and tie it into the routing and handle any distribution/storage there.

This keeps the body of your logic inside the Django consumer code, and the management command nice and simple - it just needs to send onto a custom channel and not worry about what to do with that data, you can hook that up later. If you have fields in the message you split out into different dictionary keys, you can even use the Channels routing options on those fields - so, for example, you could route everything with a certain task name to one consumer, and everything else to a catch-all consumer.

Andrew

On Wed, Feb 22, 2017 at 6:55 AM, Jochen Breuer <bre...@gmail.com> wrote:
Hi!

I'm not entirely sure where to start and I hope you can help me. I'm using SaltStack to execute tasks on minions. The results of those jobs (that's the term used in Salt) are then published to a job cache. In this case its a Redis server (Redis Pubsub). Now I would like to subscribe to a specific Redis channel, where the job results are published, with my Django (Channels) application. Every time a job result is pushed, I'd also like to push a message to a channel in Django.

Where to begin? Do I need to write a new protocol server or just a custom channel? Even after reading the docs I'm still lacking overview. Perhaps someone can push me into the right direction. Thanks!

Jochen

--
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...@googlegroups.com.
To post to this group, send email to django...@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/c933a5b5-7b72-4d7d-985a-a311c0f16b26%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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+unsubscribe@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/2bf19a22-c6e7-4a36-80b4-f06c77528437%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: Architecting a Crypto Market Data Feed using Django Channels


Thanks for your reply Andrew. I've created a custom manage command called `websocket_producer.py` that is responsible for feeding the firehose of market data (price changes, market volatility etc).

It looks like this

from django.core.management.base import BaseCommand, CommandError


from lomond.websocket import WebSocket


import channels.layers
from asgiref.sync import async_to_sync




class Command(BaseCommand):


   
def handle(self, *args, **kwargs):
        channel_layer
= channels.layers.get_channel_layer()
       
while True:
            ws
= WebSocket('wss://<my-url>/ws')
           
# this for loop is effectively infinite
           
for event in ws:
               
if event.name == "text":
                    data
= event.json
                    async_to_sync
(channel_layer.send)(
                       
'abc',
                       
{'type': 'abc', 'message': str(data)}
                   
)



However, this can only run for 1 min before resulting in the following error:


Traceback (most recent call last):
 
File "./manage.py", line 21, in <module>
    execute_from_command_line
(sys.argv)
 
File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility
.execute()
 
File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
   
self.fetch_command(subcommand).run_from_argv(self.argv)
 
File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
   
self.execute(*args, **cmd_options)
 
File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
    output
= self.handle(*args, **options)
 
File "/app/savings_apps/accounts/management/commands/wsproducer.py", line 22, in handle
   
{'type': 'action_reports', 'message': str(data)}
 
File "/usr/local/lib/python3.6/site-packages/asgiref/sync.py", line 64, in __call__
   
return call_result.result()
 
File "/usr/local/lib/python3.6/concurrent/futures/_base.py", line 398, in result
   
return self.__get_result()
 
File "/usr/local/lib/python3.6/concurrent/futures/_base.py", line 357, in __get_result
   
raise self._exception
 
File "/usr/local/lib/python3.6/site-packages/asgiref/sync.py", line 78, in main_wrap
    result
= await self.awaitable(*args, **kwargs)
 
File "/usr/local/lib/python3.6/site-packages/channels_redis/core.py", line 136, in send
   
raise ChannelFull()
channels
.exceptions.ChannelFull




Any ideas? Doing a real-time market data feed seems like it would be a pretty common use case, but I can't seem to find any prior art in Django Channels.


On Saturday, April 28, 2018 at 4:22:20 AM UTC-6, Andrew Godwin wrote:
I can't help you with real-time streaming architecture overall - that's a much bigger scope of thing - but I can say that you shouldn't be keeping a synchronous consumer open like that (you're using a whole thread). You should either rewrite it to be async-native, so it doesn't use up a thread and potentially block the server, or rework it to put the feed events onto the channel layer from an external process.

Andrew

On Sat, Apr 28, 2018 at 1:12 AM, Michael <writemicha...@gmail.com> wrote:
Hi,

What is the best way to architect a Django Channels app that provides a very fast infinite stream of market data? This is what I have so far, but I think it's not the best solution.

This data is updated every millisecond so I would prefer to not persist it (unless there is a way of using redis pub/sub without actually saving the data, only for messaging)




class ChatConsumer(WebsocketConsumer):
    def connect(self):
        self.room_name = 'foo'
        self.room_group_name = 'foo'
        async_to_sync(self.channel_layer.group_add)(
            self.room_group_name,
            self.channel_name
        )


        self.accept()
        while True:
          # Imagine this is another WS feed or Zero MQ Feed.
          feed = Feed(....)
          for event in feed:
              if event.name == "text":
                  data = event.json
                  self.send(str(data)

--
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...@googlegroups.com.
To post to this group, send email to django...@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/01bf458c-ff1a-4cf6-bd58-da9b2f43123c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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+unsubscribe@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/381ae5ed-95c4-4d0e-a194-5df291d77def%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: committing migration files

At least for me I have custom migrations (for a materialized view) so that needs to be commited, and a migration seems the most logical place to do so

Also if you already have a production database and you need to execute a complex change in the data model you may need a sequence of steps that include for example running migrations to a certain point, run a script, and then run the rest of the migrations, which can't be done if the server just makes its own migration in one step.

There may be other reasons but those are the most common for me

On Mon, Apr 30, 2018 at 1:03 PM Tom Scrace <t.scrace@gmail.com> wrote:
Hi all,

The way I've always worked, developers run `makemigrations` locally, and commit migration files to the repo.

However, a suggestion has recently come my way that a better strategy is to only commit model changes to the repo (leaving the migrations directory gitignored), and then run `makemigrations` in whatever environment the app needs to run, whether that's another developer's laptop, or in production. The putative benefit is that this avoids the possibility of migration conflicts, and always guarantees a clean migration from the state of a database schema to the state of the Django models.

Googling around, this opinion seems to be moderately widespread, although the official docs, and the *majority* of commenters seem to agree that committing migration files is the right way.

I think I agree, but I can't quite put my finger on decisive arguments against the idea of not committing migration files. I feel like I am missing something.

What are some reasons we should go the route of committing migration files?

Thanks,
Tom

--
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+unsubscribe@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/08ef20a1-9b51-4bea-a1d8-7d3ed61093fe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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+unsubscribe@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/CALn3ei1JmrBG08kBqKVNeK_ExGDUgjEe%2Bg-J9yuUU9Yt341RkQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Re: Dynamically altering a (ForeignKey) ModelChoiceField’s values

If anyone is interested in a solution I found, it is at www.webforefront.com/django/formprocessing.html. Great documentation there, and you can read why it works, too.

Here are some code snippets used to test my situation.

In forms.py and in a unique form for update (not create) - JimtestUpdateForm:
    def __init__(self, *args, **kwargs):
        super(JimtestUpdateForm, self).__init__(*args, **kwargs) # invoke the default first
        initial_arguments = kwargs.get('initial', None)
        if initial_arguments:
            ci_id = initial_arguments.get('ci_id', None)
            if ci_id:
                assign_qs = Checkin.objects.values('choretime').filter(choretime_id__isnull=False)
                unassign_qs = ChoreTime.objects.exclude(pk__in=assign_qs)
                ci_qs = Checkin.objects.get(id=ci_id)
                ct_qs = ChoreTime.objects.filter(id=ci_qs.choretime_id)
                new_qs = unassign_qs | ct_qs # union unassigned with the client's single assignment
                self.fields['choretime'].queryset = new_qs # update the choices shown
                self.fields['choretime'].widget.choices = self.fields['choretime'].choices # update the widget (may not need this - just a hunch - not tested omitting it yet

In views.py under the CB dispatch method:
        if request.method == 'GET':    
form = JimtestUpdateForm(instance=checkin, initial={'ci_id': checkinid}) # pass in the value(s) needed in __init__()
...

That is pretty much it - simple if you have the proper documentation on how to initialize forms.

Jim

On Apr 27, 2018, at 11:39 AM, Jim Illback <subaru_87@hotmail.com> wrote:

Absolutely agree. The "instance" (your note) is for Checkin and it includes a ForeignKey field to ChoreTime, which is a selection (choice field). But, I want that choice field to be limited for the forms to be only what has been not assigned yet. That way the user on the page will know that any shown pick is available. Otherwise, there are over 100 choices and no guidance as to what is truly available or already filled.

I have to alter the query set for that field. On a CreateView, I have it working perfectly (it's shown below). However, on an UpdateView, the person's choice is one of the "unavailable". So, I'm trying to add just that one choice back into the query set before showing it on the form. Therefore, I need both the URL (to get the update's choice) and the pre-form display method override (to exclude assigned entries) where I can override the field's query set.

Thanks for the inputs, Matthew. I hope I've made it clearer. If you think there's a better overall approach, I'm all ears!
Jim

On Apr 27, 2018, at 11:01 AM, Matthew Pava <Matthew.Pava@iss.com> wrote:

That ID in the URL should be taken care of in the view, not the form.  You pass the object in as keyword instance to instantiate the form.  How are you defining your form in the view?
 
Typically, you would do something like this:
 
instance = CheckIn.objects.get(pk=self.kwargs.get('pk'))
form = CheckInForm(request.POST or None, instance=instance)
 
From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On Behalf Of Jim Illback
Sent: Friday, April 27, 2018 11:33 AM
To: Django users
Subject: Re: Dynamically altering a (ForeignKey) ModelChoiceField's values
 
I also need to have the specific ID specified in the URL, so that is the other part needed. I've successfully done your suggestion for CreateView. Now, I just can't seem to get UpdateView to work properly.
 
Thanks much!
Jim


On Apr 27, 2018, at 9:20 AM, Matthew Pava <Matthew.Pava@iss.com> wrote:
 
In the form's __init__ method, you'll have to modify the queryset of the field.
 
def __init__(self, *args, **kwargs):
                super().__init__(*args, **kwargs)
                self.fields['choretime'].queryset |= ChoiceTime.objects.filter(pk=self.instance.pk)
 
I hope that helps!
 
From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On Behalf Of Jim Illback
Sent: Friday, April 27, 2018 11:16 AM
To: Django users
Subject: Re: Dynamically altering a (ForeignKey) ModelChoiceField's values
 
To make this easier, here is just a simple question. 
 
Does Django give a method that can be overridden while processing a form which has access to the incoming URL parameters and precedes the display of the form's fields, specifically being able to alter the fields's query set before it is displayed?
 
Thanks for any help to answer this question.
 
Jim Illback



On Apr 25, 2018, at 10:41 AM, Jim Illback <subaru_87@hotmail.com> wrote:
 
I wondered if anyone has had to alter the queryset behind a model form's foreign key field which presents as a model choice field?
Briefly, I have a client attribute table with the foreign key to a chore/time table. For an add of the client attribute table, I want to limit entries to unassigned chore/time combinations only. This works perfectly during my CreateView. Here are some extracts to show specifics:
Models.py:
class ChoreTime(models.Model):
chore = models.ForeignKey(Chore, on_delete=models.CASCADE)
time = models.ForeignKey(TimePeriod, on_delete=models.CASCADE)
priority = models.BooleanField(default=False)
…
class Checkin(models.Model):
client = models.ForeignKey(Client, on_delete=models.CASCADE)
choretime = models.ForeignKey(ChoreTime, on_delete=models.CASCADE)
…
Forms.py:
class CheckinForm(forms.ModelForm):
assigned_qs = Checkin.objects.values('choretime').filter(choretime_id__isnull=False)
choretime = forms.ModelChoiceField(queryset=ChoreTime.objects.exclude(pk__in=assigned_qs))
…
However, I cannot get the any design to work on an UpdateView form. Using the same form as above, the current value does not even show up – it is blank – because, of course, that entry is already assigned so is excluded.
What I'd like is to have the above exclusions BUT be able to also add in the single entry assigned to the client being updated – so the entry will show that specific assignment (as well as limiting any possible change options to unassigned values - just like on the Create).
The problems with various approaches I've tried are:

1.     Anything done before the form is fully assembled won't have the existing form's Checkin ID value (which is part of the URL, just BTW). This is needed to look up and add the existing entry. So, having a database view won't work without being able to communicate the existing person's assignment ID to the view. Similarly, using an override queryset on the form, like done above for the Create, needs that ID, too.

2.     If I do the queries in the class's GET method routine as ORM objects, I must use UNION (a union of the exclusions as above plus the existing update client's assignment). That union option keeps giving an error that one of the ORM querysets is not a queryset. However, they are both using "<class>.objects.filter…". It seems like complex queries don't work with the union command. If I use raw SQL, the query works but the assignment to the choretime field's queryset fails.

Does anyone have experience with this sort of behavior and be willing to give guidance?
 
-- 
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+unsubscribe@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/F0B95720-C669-4CE5-912E-5167E3B89DFE%40hotmail.com.
For more options, visit https://groups.google.com/d/optout.
 
-- 
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+unsubscribe@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/C42DA3B1-C882-4889-995B-49580840F539%40hotmail.com.
For more options, visit https://groups.google.com/d/optout.
 
-- 
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+unsubscribe@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/b73cf0c772af40969ec471acff9ec667%40ISS1.ISS.LOCAL.
For more options, visit 
https://groups.google.com/d/optout.
 
-- 
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+unsubscribe@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/7B3E1F75-3393-45E1-9E69-2C73FC461F56%40hotmail.com.
For more options, visit https://groups.google.com/d/optout.

-- 
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+unsubscribe@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/a61dfe45f4524aad8e73fcdb6b14b3b4%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.