Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
Django model design help

Hello,

I have a web app which I'm planning to monetize soon and I'm looking for a little guidance setting up the models. The payment system is Stripe. My current models are as follows:

**UserProfile (truncated for brevity):**


class UserProfile(models.Model):

yrs_experience = (
#Choices...
)

tradingstyles = (
#Choices...
)

analysisstyles = (
#Choices...
)

instruments = (
#Choices...
)

user = models.OneToOneField(User, on_delete=models.CASCADE)
location = models.CharField(max_length=30, blank=True)
birthdate = models.DateField(null=True, blank=True)
trading_style = models.CharField(max_length=30, choices=tradingstyles, default='NS')
analysis_style = models.CharField(max_length=30, choices=analysisstyles, default='NS')
instrument = models.CharField(max_length=30, choices=instruments, default='NS')
experience = models.CharField(max_length=10, choices=yrs_experience, default='NS')


def __str__(self): # __unicode__ for Python 2
return self.user.username


**PlanProfile (truncated for brevity):**

class PlanProfile(models.Model):

plans = (
#Choices...
)

user = models.OneToOneField(User, on_delete=models.CASCADE)
stripe_id = models.CharField(max_length=30, default='NA')
sub_id = models.CharField(max_length=30, default='NA')
is_premium = models.BooleanField(default=False)
plan_type = models.CharField(max_length=15, choices=plans, default='FREE')

def __str__(self): # __unicode__ for Python 2
return self.user.username

Currently when a customer signs up for a paid plan, the PlanProfile is updated to reflect the customer ID and subscription ID returned from Stripe, as well as setting the premium flag to true and setting their plan type so that I can manage access control within the views (via a few custom decorators). When a customer wishes to change plans, I currently have it set up so that they have to cancel their current one and then sign up for the new one. This works fine but on the Stripe end I'm executing it by deleting the subscription and the customer and setting the IDs in my DB back to 'NA' so when they sign up again it just makes a new call to Stripe and makes a new customer and subscription and then updates the DB again with the new IDs. Again that works great BUT when I delete the customer I lose their payment history, old invoices, old charges, old subscription history, etc. So, I'd like to get around that by adding a couple models to my own database and also I plan to implement an upgrade/downgrade system with Stripe as well as an alternative to cancelling/rejoining. Either way I'd like to keep the customer's payment history in my own database just for completeness so I'm looking for a little guidance with how I should build the additional models. The components I am guessing I'll want to keep from Stripe are:

- Invoices (which contain a customer ID, subscription ID, charge ID and invoice ID)
- Charges (I just want the charge ID so I have a record of past charges I guess)
- Subscriptions/subscription history for each customer(?) (Each one has an ID, and I can either change plans within the same ID or delete/recreate the subscription with a new ID/new plan in Stripe)
- Refunds (Issued if a customer cancels the sub before expiration, and is identified by a refund ID)

**Constraints:**

- Each customer can only have one subscription at a time
- Each charge is unique to a specific invoice
- An invoice may (not sure?) have multiple charges
- A subscription ID is unique to only one customer
- A charge is unique to a specific customer
- A refund is unique to a specific charge
- Possibly others I'm not thinking of...

So, I'm not a database designer and I'm not sure if/how I should design this. I'm trying to figure out which tables/models I need, e.g. a subscriptions table, a charges table, an invoices table, etc. I most likely don't need them all and would like to keep it as simple as possible. So, any insight on how you would do it?

Thanks!

/r/django
https://redd.it/70h6bo
How to choose the directory in which a file is saved

Hi, python/django noob here. I'm currently working in a module for an app to upload fields. My problem is that i want that the user uploading the field could select the directory that he/she wants to save his/her file. Reading the [Django documentation](https://docs.djangoproject.com/en/2.1/ref/models/fields/#choices) i came with the idea use the field.choices, and using that string to put it in the upload\_to.

Needless to say, my code didn't work, and I was wondering if it is even the right approach to solve my problem. Any help would be very much appreciated.

Also, i apologize for my bad english, it's not my first language.

​

​

/r/django
https://redd.it/a0m3l2